Path: blob/master/test/hotspot/gtest/runtime/test_os_linux.cpp
41144 views
/*1* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include "precompiled.hpp"2425#ifdef LINUX2627#include <sys/mman.h>2829#include "runtime/globals.hpp"30#include "runtime/os.hpp"31#include "utilities/align.hpp"32#include "concurrentTestRunner.inline.hpp"33#include "unittest.hpp"3435namespace {36static void small_page_write(void* addr, size_t size) {37size_t page_size = os::vm_page_size();3839char* end = (char*)addr + size;40for (char* p = (char*)addr; p < end; p += page_size) {41*p = 1;42}43}4445class HugeTlbfsMemory : private ::os::Linux {46char* const _ptr;47const size_t _size;48public:49static char* reserve_memory_special_huge_tlbfs(size_t bytes, size_t alignment, size_t page_size, char* req_addr, bool exec) {50return os::Linux::reserve_memory_special_huge_tlbfs(bytes, alignment, page_size, req_addr, exec);51}52HugeTlbfsMemory(char* const ptr, size_t size) : _ptr(ptr), _size(size) { }53~HugeTlbfsMemory() {54if (_ptr != NULL) {55os::Linux::release_memory_special_huge_tlbfs(_ptr, _size);56}57}58};5960class ShmMemory : private ::os::Linux {61char* const _ptr;62const size_t _size;63public:64static char* reserve_memory_special_shm(size_t bytes, size_t alignment, char* req_addr, bool exec) {65return os::Linux::reserve_memory_special_shm(bytes, alignment, req_addr, exec);66}67ShmMemory(char* const ptr, size_t size) : _ptr(ptr), _size(size) { }68~ShmMemory() {69os::Linux::release_memory_special_shm(_ptr, _size);70}71};7273// have to use these functions, as gtest's _PRED macros don't like is_aligned74// nor (is_aligned<size_t, size_t>)75static bool is_size_aligned(size_t size, size_t alignment) {76return is_aligned(size, alignment);77}78static bool is_ptr_aligned(char* ptr, size_t alignment) {79return is_aligned(ptr, alignment);80}8182static void test_reserve_memory_special_shm(size_t size, size_t alignment) {83ASSERT_TRUE(UseSHM) << "must be used only when UseSHM is true";84char* addr = ShmMemory::reserve_memory_special_shm(size, alignment, NULL, false);85if (addr != NULL) {86ShmMemory mr(addr, size);87EXPECT_PRED2(is_ptr_aligned, addr, alignment);88EXPECT_PRED2(is_ptr_aligned, addr, os::large_page_size());8990small_page_write(addr, size);91}92}93}9495TEST_VM(os_linux, reserve_memory_special_huge_tlbfs_size_aligned) {96if (!UseHugeTLBFS) {97return;98}99size_t lp = os::large_page_size();100101for (size_t size = lp; size <= lp * 10; size += lp) {102char* addr = HugeTlbfsMemory::reserve_memory_special_huge_tlbfs(size, lp, lp, NULL, false);103104if (addr != NULL) {105HugeTlbfsMemory mr(addr, size);106small_page_write(addr, size);107}108}109}110111TEST_VM(os_linux, reserve_memory_special_huge_tlbfs_size_not_aligned_without_addr) {112if (!UseHugeTLBFS) {113return;114}115size_t lp = os::large_page_size();116size_t ag = os::vm_allocation_granularity();117118// sizes to test119const size_t sizes[] = {120lp, lp + ag, lp + lp / 2, lp * 2,121lp * 2 + ag, lp * 2 - ag, lp * 2 + lp / 2,122lp * 10, lp * 10 + lp / 2123};124const int num_sizes = sizeof(sizes) / sizeof(size_t);125for (int i = 0; i < num_sizes; i++) {126const size_t size = sizes[i];127for (size_t alignment = ag; is_size_aligned(size, alignment); alignment *= 2) {128char* p = HugeTlbfsMemory::reserve_memory_special_huge_tlbfs(size, alignment, lp, NULL, false);129if (p != NULL) {130HugeTlbfsMemory mr(p, size);131EXPECT_PRED2(is_ptr_aligned, p, alignment) << " size = " << size;132small_page_write(p, size);133}134}135}136}137138TEST_VM(os_linux, reserve_memory_special_huge_tlbfs_size_not_aligned_with_good_req_addr) {139if (!UseHugeTLBFS) {140return;141}142size_t lp = os::large_page_size();143size_t ag = os::vm_allocation_granularity();144145// sizes to test146const size_t sizes[] = {147lp, lp + ag, lp + lp / 2, lp * 2,148lp * 2 + ag, lp * 2 - ag, lp * 2 + lp / 2,149lp * 10, lp * 10 + lp / 2150};151const int num_sizes = sizeof(sizes) / sizeof(size_t);152153// Pre-allocate an area as large as the largest allocation154// and aligned to the largest alignment we will be testing.155const size_t mapping_size = sizes[num_sizes - 1] * 2;156char* const mapping = (char*) ::mmap(NULL, mapping_size,157PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE,158-1, 0);159ASSERT_TRUE(mapping != MAP_FAILED) << " mmap failed, mapping_size = " << mapping_size;160// Unmap the mapping, it will serve as a value for a "good" req_addr161::munmap(mapping, mapping_size);162163for (int i = 0; i < num_sizes; i++) {164const size_t size = sizes[i];165for (size_t alignment = ag; is_size_aligned(size, alignment); alignment *= 2) {166// req_addr must be at least large page aligned.167char* const req_addr = align_up(mapping, MAX2(alignment, lp));168char* p = HugeTlbfsMemory::reserve_memory_special_huge_tlbfs(size, alignment, lp, req_addr, false);169if (p != NULL) {170HugeTlbfsMemory mr(p, size);171ASSERT_EQ(req_addr, p) << " size = " << size << ", alignment = " << alignment;172small_page_write(p, size);173}174}175}176}177178179TEST_VM(os_linux, reserve_memory_special_huge_tlbfs_size_not_aligned_with_bad_req_addr) {180if (!UseHugeTLBFS) {181return;182}183size_t lp = os::large_page_size();184size_t ag = os::vm_allocation_granularity();185186// sizes to test187const size_t sizes[] = {188lp, lp + ag, lp + lp / 2, lp * 2,189lp * 2 + ag, lp * 2 - ag, lp * 2 + lp / 2,190lp * 10, lp * 10 + lp / 2191};192const int num_sizes = sizeof(sizes) / sizeof(size_t);193194// Pre-allocate an area as large as the largest allocation195// and aligned to the largest alignment we will be testing.196const size_t mapping_size = sizes[num_sizes - 1] * 2;197char* const mapping = (char*) ::mmap(NULL, mapping_size,198PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE,199-1, 0);200ASSERT_TRUE(mapping != MAP_FAILED) << " mmap failed, mapping_size = " << mapping_size;201// Leave the mapping intact, it will server as "bad" req_addr202203class MappingHolder {204char* const _mapping;205size_t _size;206public:207MappingHolder(char* mapping, size_t size) : _mapping(mapping), _size(size) { }208~MappingHolder() {209::munmap(_mapping, _size);210}211} holder(mapping, mapping_size);212213for (int i = 0; i < num_sizes; i++) {214const size_t size = sizes[i];215for (size_t alignment = ag; is_size_aligned(size, alignment); alignment *= 2) {216// req_addr must be at least large page aligned.217char* const req_addr = align_up(mapping, MAX2(alignment, lp));218char* p = HugeTlbfsMemory::reserve_memory_special_huge_tlbfs(size, alignment, lp, req_addr, false);219HugeTlbfsMemory mr(p, size);220// as the area around req_addr contains already existing mappings, the API should always221// return NULL (as per contract, it cannot return another address)222EXPECT_TRUE(p == NULL) << " size = " << size223<< ", alignment = " << alignment224<< ", req_addr = " << req_addr225<< ", p = " << p;226}227}228}229230TEST_VM(os_linux, reserve_memory_special_shm) {231if (!UseSHM) {232return;233}234size_t lp = os::large_page_size();235size_t ag = os::vm_allocation_granularity();236237for (size_t size = ag; size < lp * 3; size += ag) {238for (size_t alignment = ag; is_size_aligned(size, alignment); alignment *= 2) {239EXPECT_NO_FATAL_FAILURE(test_reserve_memory_special_shm(size, alignment));240}241}242}243244class TestReserveMemorySpecial : AllStatic {245public:246static void small_page_write(void* addr, size_t size) {247size_t page_size = os::vm_page_size();248249char* end = (char*)addr + size;250for (char* p = (char*)addr; p < end; p += page_size) {251*p = 1;252}253}254255static void test_reserve_memory_special_huge_tlbfs_size_aligned(size_t size, size_t alignment, size_t page_size) {256if (!UseHugeTLBFS) {257return;258}259260char* addr = os::Linux::reserve_memory_special_huge_tlbfs(size, alignment, page_size, NULL, false);261262if (addr != NULL) {263small_page_write(addr, size);264265os::Linux::release_memory_special_huge_tlbfs(addr, size);266}267}268269static void test_reserve_memory_special_huge_tlbfs_size_aligned() {270if (!UseHugeTLBFS) {271return;272}273274size_t lp = os::large_page_size();275276for (size_t size = lp; size <= lp * 10; size += lp) {277test_reserve_memory_special_huge_tlbfs_size_aligned(size, lp, lp);278}279}280281static void test_reserve_memory_special_huge_tlbfs_size_not_aligned() {282size_t lp = os::large_page_size();283size_t ag = os::vm_allocation_granularity();284285// sizes to test286const size_t sizes[] = {287lp, lp + ag, lp + lp / 2, lp * 2,288lp * 2 + ag, lp * 2 - ag, lp * 2 + lp / 2,289lp * 10, lp * 10 + lp / 2290};291const int num_sizes = sizeof(sizes) / sizeof(size_t);292293// For each size/alignment combination, we test three scenarios:294// 1) with req_addr == NULL295// 2) with a non-null req_addr at which we expect to successfully allocate296// 3) with a non-null req_addr which contains a pre-existing mapping, at which we297// expect the allocation to either fail or to ignore req_addr298299// Pre-allocate two areas; they shall be as large as the largest allocation300// and aligned to the largest alignment we will be testing.301const size_t mapping_size = sizes[num_sizes - 1] * 2;302char* const mapping1 = (char*) ::mmap(NULL, mapping_size,303PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE,304-1, 0);305EXPECT_NE(mapping1, MAP_FAILED);306307char* const mapping2 = (char*) ::mmap(NULL, mapping_size,308PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE,309-1, 0);310EXPECT_NE(mapping2, MAP_FAILED);311312// Unmap the first mapping, but leave the second mapping intact: the first313// mapping will serve as a value for a "good" req_addr (case 2). The second314// mapping, still intact, as "bad" req_addr (case 3).315::munmap(mapping1, mapping_size);316317// Case 1318for (int i = 0; i < num_sizes; i++) {319const size_t size = sizes[i];320for (size_t alignment = ag; is_aligned(size, alignment); alignment *= 2) {321char* p = os::Linux::reserve_memory_special_huge_tlbfs(size, alignment, lp, NULL, false);322if (p != NULL) {323EXPECT_TRUE(is_aligned(p, alignment));324small_page_write(p, size);325os::Linux::release_memory_special_huge_tlbfs(p, size);326}327}328}329330// Case 2331for (int i = 0; i < num_sizes; i++) {332const size_t size = sizes[i];333for (size_t alignment = ag; is_aligned(size, alignment); alignment *= 2) {334// req_addr must be at least large page aligned.335char* const req_addr = align_up(mapping1, MAX2(alignment, lp));336char* p = os::Linux::reserve_memory_special_huge_tlbfs(size, alignment, lp, req_addr, false);337if (p != NULL) {338EXPECT_EQ(p, req_addr);339small_page_write(p, size);340os::Linux::release_memory_special_huge_tlbfs(p, size);341}342}343}344345// Case 3346for (int i = 0; i < num_sizes; i++) {347const size_t size = sizes[i];348for (size_t alignment = ag; is_aligned(size, alignment); alignment *= 2) {349// req_addr must be at least large page aligned.350char* const req_addr = align_up(mapping2, MAX2(alignment, lp));351char* p = os::Linux::reserve_memory_special_huge_tlbfs(size, alignment, lp, req_addr, false);352// as the area around req_addr contains already existing mappings, the API should always353// return NULL (as per contract, it cannot return another address)354EXPECT_TRUE(p == NULL);355}356}357358::munmap(mapping2, mapping_size);359360}361362static void test_reserve_memory_special_huge_tlbfs() {363if (!UseHugeTLBFS) {364return;365}366367test_reserve_memory_special_huge_tlbfs_size_aligned();368test_reserve_memory_special_huge_tlbfs_size_not_aligned();369}370371static void test_reserve_memory_special_shm(size_t size, size_t alignment) {372if (!UseSHM) {373return;374}375376char* addr = os::Linux::reserve_memory_special_shm(size, alignment, NULL, false);377378if (addr != NULL) {379EXPECT_TRUE(is_aligned(addr, alignment));380EXPECT_TRUE(is_aligned(addr, os::large_page_size()));381382small_page_write(addr, size);383384os::Linux::release_memory_special_shm(addr, size);385}386}387388static void test_reserve_memory_special_shm() {389size_t lp = os::large_page_size();390size_t ag = os::vm_allocation_granularity();391392for (size_t size = ag; size < lp * 3; size += ag) {393for (size_t alignment = ag; is_aligned(size, alignment); alignment *= 2) {394test_reserve_memory_special_shm(size, alignment);395}396}397}398399static void test() {400test_reserve_memory_special_huge_tlbfs();401test_reserve_memory_special_shm();402}403};404405TEST_VM(os_linux, reserve_memory_special) {406TestReserveMemorySpecial::test();407}408409class ReserveMemorySpecialRunnable : public TestRunnable {410public:411void runUnitTest() const {412TestReserveMemorySpecial::test();413}414};415416TEST_VM(os_linux, reserve_memory_special_concurrent) {417if (UseLargePages) {418ReserveMemorySpecialRunnable runnable;419ConcurrentTestRunner testRunner(&runnable, 5, 3000);420testRunner.run();421}422}423424#endif425426427