Skip to content

Commit 44e6820

Browse files
committed
8253650: Cleanup: remove alignment_hint parameter from os::reserve_memory
Reviewed-by: stefank, tschatzl
1 parent ed62b01 commit 44e6820

File tree

12 files changed

+55
-87
lines changed

12 files changed

+55
-87
lines changed

src/hotspot/os/aix/os_aix.cpp

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,21 +1925,10 @@ static void vmembk_print_on(outputStream* os) {
19251925
// If <requested_addr> is not NULL, function will attempt to attach the memory at the given
19261926
// address. Failing that, it will attach the memory anywhere.
19271927
// If <requested_addr> is NULL, function will attach the memory anywhere.
1928-
//
1929-
// <alignment_hint> is being ignored by this function. It is very probable however that the
1930-
// alignment requirements are met anyway, because shmat() attaches at 256M boundaries.
1931-
// Should this be not enogh, we can put more work into it.
1932-
static char* reserve_shmated_memory (
1933-
size_t bytes,
1934-
char* requested_addr,
1935-
size_t alignment_hint) {
1928+
static char* reserve_shmated_memory (size_t bytes, char* requested_addr) {
19361929

19371930
trcVerbose("reserve_shmated_memory " UINTX_FORMAT " bytes, wishaddress "
1938-
PTR_FORMAT ", alignment_hint " UINTX_FORMAT "...",
1939-
bytes, p2i(requested_addr), alignment_hint);
1940-
1941-
// Either give me wish address or wish alignment but not both.
1942-
assert0(!(requested_addr != NULL && alignment_hint != 0));
1931+
PTR_FORMAT "...", bytes, p2i(requested_addr));
19431932

19441933
// We must prevent anyone from attaching too close to the
19451934
// BRK because that may cause malloc OOM.
@@ -2061,15 +2050,10 @@ static bool uncommit_shmated_memory(char* addr, size_t size) {
20612050
// Reserve memory via mmap.
20622051
// If <requested_addr> is given, an attempt is made to attach at the given address.
20632052
// Failing that, memory is allocated at any address.
2064-
// If <alignment_hint> is given and <requested_addr> is NULL, an attempt is made to
2065-
// allocate at an address aligned with the given alignment. Failing that, memory
2066-
// is aligned anywhere.
2067-
static char* reserve_mmaped_memory(size_t bytes, char* requested_addr, size_t alignment_hint) {
2068-
trcVerbose("reserve_mmaped_memory " UINTX_FORMAT " bytes, wishaddress " PTR_FORMAT ", "
2069-
"alignment_hint " UINTX_FORMAT "...",
2070-
bytes, p2i(requested_addr), alignment_hint);
2071-
2072-
// If a wish address is given, but not aligned to 4K page boundary, mmap will fail.
2053+
static char* reserve_mmaped_memory(size_t bytes, char* requested_addr) {
2054+
trcVerbose("reserve_mmaped_memory " UINTX_FORMAT " bytes, wishaddress " PTR_FORMAT "...",
2055+
bytes, p2i(requested_addr));
2056+
20732057
if (requested_addr && !is_aligned_to(requested_addr, os::vm_page_size()) != 0) {
20742058
trcVerbose("Wish address " PTR_FORMAT " not aligned to page boundary.", p2i(requested_addr));
20752059
return NULL;
@@ -2084,26 +2068,21 @@ static char* reserve_mmaped_memory(size_t bytes, char* requested_addr, size_t al
20842068
requested_addr = NULL;
20852069
}
20862070

2087-
// Specify one or the other but not both.
2088-
assert0(!(requested_addr != NULL && alignment_hint > 0));
2089-
2090-
// In 64K mode, we claim the global page size (os::vm_page_size())
2091-
// is 64K. This is one of the few points where that illusion may
2092-
// break, because mmap() will always return memory aligned to 4K. So
2093-
// we must ensure we only ever return memory aligned to 64k.
2094-
if (alignment_hint) {
2095-
alignment_hint = lcm(alignment_hint, os::vm_page_size());
2096-
} else {
2097-
alignment_hint = os::vm_page_size();
2098-
}
2071+
// In 64K mode, we lie and claim the global page size (os::vm_page_size()) is 64K
2072+
// (complicated story). This mostly works just fine since 64K is a multiple of the
2073+
// actual 4K lowest page size. Only at a few seams light shines thru, e.g. when
2074+
// calling mmap. mmap will return memory aligned to the lowest pages size - 4K -
2075+
// so we must make sure - transparently - that the caller only ever sees 64K
2076+
// aligned mapping start addresses.
2077+
const size_t alignment = os::vm_page_size();
20992078

21002079
// Size shall always be a multiple of os::vm_page_size (esp. in 64K mode).
21012080
const size_t size = align_up(bytes, os::vm_page_size());
21022081

21032082
// alignment: Allocate memory large enough to include an aligned range of the right size and
21042083
// cut off the leading and trailing waste pages.
2105-
assert0(alignment_hint != 0 && is_aligned_to(alignment_hint, os::vm_page_size())); // see above
2106-
const size_t extra_size = size + alignment_hint;
2084+
assert0(alignment != 0 && is_aligned_to(alignment, os::vm_page_size())); // see above
2085+
const size_t extra_size = size + alignment;
21072086

21082087
// Note: MAP_SHARED (instead of MAP_PRIVATE) needed to be able to
21092088
// later use msync(MS_INVALIDATE) (see os::uncommit_memory).
@@ -2131,7 +2110,7 @@ static char* reserve_mmaped_memory(size_t bytes, char* requested_addr, size_t al
21312110
}
21322111

21332112
// Handle alignment.
2134-
char* const addr_aligned = align_up(addr, alignment_hint);
2113+
char* const addr_aligned = align_up(addr, alignment);
21352114
const size_t waste_pre = addr_aligned - addr;
21362115
char* const addr_aligned_end = addr_aligned + size;
21372116
const size_t waste_post = extra_size - waste_pre - size;
@@ -2347,21 +2326,19 @@ char *os::scan_pages(char *start, char* end, page_info* page_expected, page_info
23472326
}
23482327

23492328
// Reserves and attaches a shared memory segment.
2350-
char* os::pd_reserve_memory(size_t bytes, size_t alignment_hint) {
2329+
char* os::pd_reserve_memory(size_t bytes) {
23512330
// Always round to os::vm_page_size(), which may be larger than 4K.
23522331
bytes = align_up(bytes, os::vm_page_size());
2353-
const size_t alignment_hint0 =
2354-
alignment_hint ? align_up(alignment_hint, os::vm_page_size()) : 0;
23552332

23562333
// In 4K mode always use mmap.
23572334
// In 64K mode allocate small sizes with mmap, large ones with 64K shmatted.
23582335
if (os::vm_page_size() == 4*K) {
2359-
return reserve_mmaped_memory(bytes, NULL /* requested_addr */, alignment_hint);
2336+
return reserve_mmaped_memory(bytes, NULL /* requested_addr */);
23602337
} else {
23612338
if (bytes >= Use64KPagesThreshold) {
2362-
return reserve_shmated_memory(bytes, NULL /* requested_addr */, alignment_hint);
2339+
return reserve_shmated_memory(bytes, NULL /* requested_addr */);
23632340
} else {
2364-
return reserve_mmaped_memory(bytes, NULL /* requested_addr */, alignment_hint);
2341+
return reserve_mmaped_memory(bytes, NULL /* requested_addr */);
23652342
}
23662343
}
23672344
}
@@ -2538,7 +2515,7 @@ char* os::pd_attempt_reserve_memory_at(char* requested_addr, size_t bytes, int f
25382515

25392516
// Always round to os::vm_page_size(), which may be larger than 4K.
25402517
bytes = align_up(bytes, os::vm_page_size());
2541-
result = reserve_mmaped_memory(bytes, requested_addr, 0);
2518+
result = reserve_mmaped_memory(bytes, requested_addr);
25422519

25432520
if (result != NULL) {
25442521
if (replace_existing_mapping_with_file_mapping(result, bytes, file_desc) == NULL) {
@@ -2559,12 +2536,12 @@ char* os::pd_attempt_reserve_memory_at(char* requested_addr, size_t bytes) {
25592536
// In 4K mode always use mmap.
25602537
// In 64K mode allocate small sizes with mmap, large ones with 64K shmatted.
25612538
if (os::vm_page_size() == 4*K) {
2562-
return reserve_mmaped_memory(bytes, requested_addr, 0);
2539+
return reserve_mmaped_memory(bytes, requested_addr);
25632540
} else {
25642541
if (bytes >= Use64KPagesThreshold) {
2565-
return reserve_shmated_memory(bytes, requested_addr, 0);
2542+
return reserve_shmated_memory(bytes, requested_addr);
25662543
} else {
2567-
return reserve_mmaped_memory(bytes, requested_addr, 0);
2544+
return reserve_mmaped_memory(bytes, requested_addr);
25682545
}
25692546
}
25702547

src/hotspot/os/bsd/os_bsd.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,8 +2030,7 @@ static int anon_munmap(char * addr, size_t size) {
20302030
return ::munmap(addr, size) == 0;
20312031
}
20322032

2033-
char* os::pd_reserve_memory(size_t bytes, size_t alignment_hint) {
2034-
// Ignores alignment hint
2033+
char* os::pd_reserve_memory(size_t bytes) {
20352034
return anon_mmap(NULL /* addr */, bytes);
20362035
}
20372036

src/hotspot/os/linux/os_linux.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3694,8 +3694,7 @@ static int anon_munmap(char * addr, size_t size) {
36943694
return ::munmap(addr, size) == 0;
36953695
}
36963696

3697-
char* os::pd_reserve_memory(size_t bytes, size_t alignment_hint) {
3698-
// Ignores alignment hint
3697+
char* os::pd_reserve_memory(size_t bytes) {
36993698
return anon_mmap(NULL, bytes);
37003699
}
37013700

src/hotspot/os/posix/os_posix.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,17 +316,18 @@ char* os::reserve_memory_aligned(size_t size, size_t alignment, int file_desc) {
316316

317317
char* extra_base;
318318
if (file_desc != -1) {
319-
// For file mapping, we do not call os:reserve_memory(extra_size, NULL, alignment, file_desc) because
320-
// we need to deal with shrinking of the file space later when we release extra memory after alignment.
321-
// We also cannot called os:reserve_memory() with file_desc set to -1 because on aix we might get SHM memory.
322-
// So here to call a helper function while reserve memory for us. After we have a aligned base,
323-
// we will replace anonymous mapping with file mapping.
319+
// For file mapping, we do not call os:reserve_memory_with_fd since:
320+
// - we later chop away parts of the mapping using os::release_memory and that could fail if the
321+
// original mmap call had been tied to an fd.
322+
// - The memory API os::reserve_memory uses is an implementation detail. It may (and usually is)
323+
// mmap but it also may System V shared memory which cannot be uncommitted as a whole, so
324+
// chopping off and unmapping excess bits back and front (see below) would not work.
324325
extra_base = reserve_mmapped_memory(extra_size, NULL);
325326
if (extra_base != NULL) {
326327
MemTracker::record_virtual_memory_reserve((address)extra_base, extra_size, CALLER_PC);
327328
}
328329
} else {
329-
extra_base = os::reserve_memory(extra_size, alignment);
330+
extra_base = os::reserve_memory(extra_size);
330331
}
331332

332333
if (extra_base == NULL) {

src/hotspot/os/windows/os_windows.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3086,7 +3086,7 @@ char* os::reserve_memory_aligned(size_t size, size_t alignment, int file_desc) {
30863086
char* aligned_base = NULL;
30873087

30883088
do {
3089-
char* extra_base = os::reserve_memory_with_fd(extra_size, alignment, file_desc);
3089+
char* extra_base = os::reserve_memory_with_fd(extra_size, file_desc);
30903090
if (extra_base == NULL) {
30913091
return NULL;
30923092
}
@@ -3106,8 +3106,7 @@ char* os::reserve_memory_aligned(size_t size, size_t alignment, int file_desc) {
31063106
return aligned_base;
31073107
}
31083108

3109-
char* os::pd_reserve_memory(size_t bytes, size_t alignment_hint) {
3110-
// Ignores alignment hint
3109+
char* os::pd_reserve_memory(size_t bytes) {
31113110
return pd_attempt_reserve_memory_at(NULL /* addr */, bytes);
31123111
}
31133112

src/hotspot/share/gc/z/zMarkStackAllocator.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ ZMarkStackSpace::ZMarkStackSpace() :
4242

4343
// Reserve address space
4444
const size_t size = ZMarkStackSpaceLimit;
45-
const size_t alignment = (size_t)os::vm_allocation_granularity();
46-
const uintptr_t addr = (uintptr_t)os::reserve_memory(size, alignment, mtGC);
45+
const uintptr_t addr = (uintptr_t)os::reserve_memory(size, mtGC);
4746
if (addr == 0) {
4847
log_error_pd(gc, marking)("Failed to reserve address space for mark stacks");
4948
return;

src/hotspot/share/memory/allocation.inline.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ size_t MmapArrayAllocator<E>::size_for(size_t length) {
5555
template <class E>
5656
E* MmapArrayAllocator<E>::allocate_or_null(size_t length, MEMFLAGS flags) {
5757
size_t size = size_for(length);
58-
int alignment = os::vm_allocation_granularity();
5958

60-
char* addr = os::reserve_memory(size, alignment, flags);
59+
char* addr = os::reserve_memory(size, flags);
6160
if (addr == NULL) {
6261
return NULL;
6362
}
@@ -73,9 +72,8 @@ E* MmapArrayAllocator<E>::allocate_or_null(size_t length, MEMFLAGS flags) {
7372
template <class E>
7473
E* MmapArrayAllocator<E>::allocate(size_t length, MEMFLAGS flags) {
7574
size_t size = size_for(length);
76-
int alignment = os::vm_allocation_granularity();
7775

78-
char* addr = os::reserve_memory(size, alignment, flags);
76+
char* addr = os::reserve_memory(size, flags);
7977
if (addr == NULL) {
8078
vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "Allocator (reserve)");
8179
}

src/hotspot/share/memory/virtualspace.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void ReservedSpace::initialize(size_t size, size_t alignment, bool large,
179179
}
180180

181181
if (base == NULL) {
182-
// Optimistically assume that the OSes returns an aligned base pointer.
182+
// Optimistically assume that the OS returns an aligned base pointer.
183183
// When reserving a large address range, most OSes seem to align to at
184184
// least 64K.
185185

@@ -194,7 +194,7 @@ void ReservedSpace::initialize(size_t size, size_t alignment, bool large,
194194
base = NULL;
195195
}
196196
} else {
197-
base = os::reserve_memory_with_fd(size, alignment, _fd_for_heap);
197+
base = os::reserve_memory_with_fd(size, _fd_for_heap);
198198
}
199199

200200
if (base == NULL) return;
@@ -371,18 +371,14 @@ void ReservedHeapSpace::try_reserve_heap(size_t size,
371371
log_debug(gc, heap, coops)("Reserve regular memory without large pages");
372372
}
373373

374-
// Optimistically assume that the OSes returns an aligned base pointer.
375-
// When reserving a large address range, most OSes seem to align to at
376-
// least 64K.
377-
378-
// If the memory was requested at a particular address, use
379-
// os::attempt_reserve_memory_at() to avoid over mapping something
380-
// important. If available space is not detected, return NULL.
381-
382374
if (requested_address != 0) {
383375
base = os::attempt_reserve_memory_at(requested_address, size, _fd_for_heap);
384376
} else {
385-
base = os::reserve_memory_with_fd(size, alignment, _fd_for_heap);
377+
// Optimistically assume that the OSes returns an aligned base pointer.
378+
// When reserving a large address range, most OSes seem to align to at
379+
// least 64K.
380+
// If the returned memory is not aligned we will release and retry.
381+
base = os::reserve_memory_with_fd(size, _fd_for_heap);
386382
}
387383
}
388384
if (base == NULL) { return; }

src/hotspot/share/runtime/os.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,8 +1652,8 @@ bool os::create_stack_guard_pages(char* addr, size_t bytes) {
16521652
return os::pd_create_stack_guard_pages(addr, bytes);
16531653
}
16541654

1655-
char* os::reserve_memory(size_t bytes, size_t alignment_hint, MEMFLAGS flags) {
1656-
char* result = pd_reserve_memory(bytes, alignment_hint);
1655+
char* os::reserve_memory(size_t bytes, MEMFLAGS flags) {
1656+
char* result = pd_reserve_memory(bytes);
16571657
if (result != NULL) {
16581658
MemTracker::record_virtual_memory_reserve(result, bytes, CALLER_PC);
16591659
if (flags != mtOther) {
@@ -1664,7 +1664,7 @@ char* os::reserve_memory(size_t bytes, size_t alignment_hint, MEMFLAGS flags) {
16641664
return result;
16651665
}
16661666

1667-
char* os::reserve_memory_with_fd(size_t bytes, size_t alignment_hint, int file_desc) {
1667+
char* os::reserve_memory_with_fd(size_t bytes, int file_desc) {
16681668
char* result;
16691669

16701670
if (file_desc != -1) {
@@ -1675,7 +1675,7 @@ char* os::reserve_memory_with_fd(size_t bytes, size_t alignment_hint, int file_d
16751675
MemTracker::record_virtual_memory_reserve_and_commit(result, bytes, CALLER_PC);
16761676
}
16771677
} else {
1678-
result = pd_reserve_memory(bytes, alignment_hint);
1678+
result = pd_reserve_memory(bytes);
16791679
if (result != NULL) {
16801680
MemTracker::record_virtual_memory_reserve(result, bytes, CALLER_PC);
16811681
}

src/hotspot/share/runtime/os.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class os: AllStatic {
113113
_page_sizes[1] = 0; // sentinel
114114
}
115115

116-
static char* pd_reserve_memory(size_t bytes, size_t alignment_hint);
116+
static char* pd_reserve_memory(size_t bytes);
117117

118118
static char* pd_attempt_reserve_memory_at(char* addr, size_t bytes);
119119
static char* pd_attempt_reserve_memory_at(char* addr, size_t bytes, int file_desc);
@@ -314,11 +314,11 @@ class os: AllStatic {
314314

315315
// Reserves virtual memory.
316316
// alignment_hint - currently only used by AIX
317-
static char* reserve_memory(size_t bytes, size_t alignment_hint = 0, MEMFLAGS flags = mtOther);
317+
static char* reserve_memory(size_t bytes, MEMFLAGS flags = mtOther);
318318

319319
// Reserves virtual memory.
320320
// if file_desc != -1, also attaches the memory to the file.
321-
static char* reserve_memory_with_fd(size_t bytes, size_t alignment_hint, int file_desc);
321+
static char* reserve_memory_with_fd(size_t bytes, int file_desc);
322322

323323
// Reserves virtual memory that starts at an address that is aligned to 'alignment'.
324324
static char* reserve_memory_aligned(size_t size, size_t alignment, int file_desc = -1);

src/hotspot/share/runtime/safepointMechanism.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void SafepointMechanism::default_initialize() {
4747
// Polling page
4848
const size_t page_size = os::vm_page_size();
4949
const size_t allocation_size = 2 * page_size;
50-
char* polling_page = os::reserve_memory(allocation_size, page_size);
50+
char* polling_page = os::reserve_memory(allocation_size);
5151
os::commit_memory_or_exit(polling_page, allocation_size, false, "Unable to commit Safepoint polling page");
5252
MemTracker::record_virtual_memory_type((address)polling_page, mtSafepoint);
5353

test/hotspot/gtest/runtime/test_committed_virtualmemory.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class CommittedVirtualMemoryTest {
101101
static void test_committed_region_impl(size_t num_pages, size_t touch_pages, int* page_num) {
102102
const size_t page_sz = os::vm_page_size();
103103
const size_t size = num_pages * page_sz;
104-
char* base = os::reserve_memory(size, page_sz, mtThreadStack);
104+
char* base = os::reserve_memory(size, mtThreadStack);
105105
bool result = os::commit_memory(base, size, false);
106106
size_t index;
107107
ASSERT_NE(base, (char*)NULL);
@@ -169,7 +169,7 @@ class CommittedVirtualMemoryTest {
169169
const size_t page_sz = os::vm_page_size();
170170
const size_t num_pages = 4;
171171
const size_t size = num_pages * page_sz;
172-
char* base = os::reserve_memory(size, page_sz, mtTest);
172+
char* base = os::reserve_memory(size, mtTest);
173173
ASSERT_NE(base, (char*)NULL);
174174
result = os::commit_memory(base, size, false);
175175

0 commit comments

Comments
 (0)