Skip to content

Commit f639df4

Browse files
committedFeb 17, 2021
8261401: Add sanity check for UseSHM large pages similar to the one used with hugetlb large pages
Reviewed-by: stuefe, tschatzl
1 parent 2e18b52 commit f639df4

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed
 

‎src/hotspot/os/linux/os_linux.cpp

+41-4
Original file line numberDiff line numberDiff line change
@@ -3564,6 +3564,30 @@ bool os::Linux::hugetlbfs_sanity_check(bool warn, size_t page_size) {
35643564
return result;
35653565
}
35663566

3567+
bool os::Linux::shm_hugetlbfs_sanity_check(bool warn, size_t page_size) {
3568+
// Try to create a large shared memory segment.
3569+
int shmid = shmget(IPC_PRIVATE, page_size, SHM_HUGETLB|IPC_CREAT|SHM_R|SHM_W);
3570+
if (shmid == -1) {
3571+
// Possible reasons for shmget failure:
3572+
// 1. shmmax is too small for the request.
3573+
// > check shmmax value: cat /proc/sys/kernel/shmmax
3574+
// > increase shmmax value: echo "new_value" > /proc/sys/kernel/shmmax
3575+
// 2. not enough large page memory.
3576+
// > check available large pages: cat /proc/meminfo
3577+
// > increase amount of large pages:
3578+
// sysctl -w vm.nr_hugepages=new_value
3579+
// > For more information regarding large pages please refer to:
3580+
// https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt
3581+
if (warn) {
3582+
warning("Large pages using UseSHM are not configured on this system.");
3583+
}
3584+
return false;
3585+
}
3586+
// Managed to create a segment, now delete it.
3587+
shmctl(shmid, IPC_RMID, NULL);
3588+
return true;
3589+
}
3590+
35673591
// From the coredump_filter documentation:
35683592
//
35693593
// - (bit 0) anonymous private memory
@@ -3748,7 +3772,18 @@ bool os::Linux::setup_large_page_type(size_t page_size) {
37483772
UseHugeTLBFS = false;
37493773
}
37503774

3751-
return UseSHM;
3775+
if (UseSHM) {
3776+
bool warn_on_failure = !FLAG_IS_DEFAULT(UseSHM);
3777+
if (shm_hugetlbfs_sanity_check(warn_on_failure, page_size)) {
3778+
return true;
3779+
}
3780+
UseSHM = false;
3781+
}
3782+
3783+
if (!FLAG_IS_DEFAULT(UseLargePages)) {
3784+
log_warning(pagesize)("UseLargePages disabled, no large pages configured and available on the system.");
3785+
}
3786+
return false;
37523787
}
37533788

37543789
void os::large_page_init() {
@@ -3888,13 +3923,15 @@ char* os::Linux::reserve_memory_special_shm(size_t bytes, size_t alignment,
38883923
int shmid = shmget(IPC_PRIVATE, bytes, SHM_HUGETLB|IPC_CREAT|SHM_R|SHM_W);
38893924
if (shmid == -1) {
38903925
// Possible reasons for shmget failure:
3891-
// 1. shmmax is too small for Java heap.
3926+
// 1. shmmax is too small for the request.
38923927
// > check shmmax value: cat /proc/sys/kernel/shmmax
3893-
// > increase shmmax value: echo "0xffffffff" > /proc/sys/kernel/shmmax
3928+
// > increase shmmax value: echo "new_value" > /proc/sys/kernel/shmmax
38943929
// 2. not enough large page memory.
38953930
// > check available large pages: cat /proc/meminfo
38963931
// > increase amount of large pages:
3897-
// echo new_value > /proc/sys/vm/nr_hugepages
3932+
// sysctl -w vm.nr_hugepages=new_value
3933+
// > For more information regarding large pages please refer to:
3934+
// https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt
38983935
// Note 1: different Linux may use different name for this property,
38993936
// e.g. on Redhat AS-3 it is "hugetlb_pool".
39003937
// Note 2: it's possible there's enough physical memory available but

‎src/hotspot/os/linux/os_linux.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class Linux {
8585
static bool setup_large_page_type(size_t page_size);
8686
static bool transparent_huge_pages_sanity_check(bool warn, size_t pages_size);
8787
static bool hugetlbfs_sanity_check(bool warn, size_t page_size);
88+
static bool shm_hugetlbfs_sanity_check(bool warn, size_t page_size);
8889

8990
static char* reserve_memory_special_shm(size_t bytes, size_t alignment, char* req_addr, bool exec);
9091
static char* reserve_memory_special_huge_tlbfs(size_t bytes, size_t alignment, char* req_addr, bool exec);

‎test/hotspot/jtreg/gc/g1/TestLargePageUseForHeap.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ static void checkSize(OutputAnalyzer output, long expectedSize, String pattern)
6161
}
6262

6363
static boolean checkLargePageEnabled(OutputAnalyzer output) {
64+
String lp = output.firstMatch("Large Page Support: (\\w*)", 1);
65+
// Make sure large pages really are enabled.
66+
if (lp == null || lp.equals("Disabled")) {
67+
return false;
68+
}
6469
// This message is printed when tried to reserve a memory with large page but it failed.
6570
String errorStr = "Reserve regular memory without large pages";
6671
String heapPattern = ".*Heap: ";
@@ -84,7 +89,7 @@ static void testVM(long regionSize) throws Exception {
8489
pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
8590
"-XX:G1HeapRegionSize=" + regionSize,
8691
"-Xmx128m",
87-
"-Xlog:pagesize,gc+heap+coops=debug",
92+
"-Xlog:gc+init,pagesize,gc+heap+coops=debug",
8893
"-XX:+UseLargePages",
8994
"-version");
9095

@@ -97,7 +102,7 @@ static void testVM(long regionSize) throws Exception {
97102
pb = ProcessTools.createJavaProcessBuilder("-XX:+UseG1GC",
98103
"-XX:G1HeapRegionSize=" + regionSize,
99104
"-Xmx128m",
100-
"-Xlog:pagesize,gc+heap+coops=debug",
105+
"-Xlog:gc+init,pagesize,gc+heap+coops=debug",
101106
"-XX:-UseLargePages",
102107
"-version");
103108

0 commit comments

Comments
 (0)
Please sign in to comment.