Skip to content

Commit 75e46aa

Browse files
committed
Backport 501e6aed4407d63b000320168dc5d0553ce8a23b
1 parent f672a4c commit 75e46aa

File tree

4 files changed

+121
-2
lines changed

4 files changed

+121
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1936,7 +1936,7 @@ void ZPageAllocator::cleanup_failed_commit_multi_partition(ZMultiPartitionAlloca
19361936
}
19371937

19381938
const size_t committed = allocation->committed_capacity();
1939-
const ZVirtualMemory non_harvested_vmem = vmem.last_part(allocation->harvested());
1939+
const ZVirtualMemory non_harvested_vmem = partial_vmem.last_part(allocation->harvested());
19401940
const ZVirtualMemory committed_vmem = non_harvested_vmem.first_part(committed);
19411941
const ZVirtualMemory non_committed_vmem = non_harvested_vmem.last_part(committed);
19421942

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,20 @@ void ZPhysicalMemoryManager::free(const ZVirtualMemory& vmem, uint32_t numa_id)
214214
});
215215
}
216216

217+
static size_t inject_commit_limit(const ZVirtualMemory& vmem) {
218+
// To facilitate easier interoperability with multi partition allocations we
219+
// divide by ZNUMA::count(). Users of ZFailLargerCommits need to be aware of
220+
// this when writing tests. In the future we could probe the VirtualMemoryManager
221+
// and condition this division on whether the vmem is in the multi partition
222+
// address space.
223+
return align_up(MIN2(ZFailLargerCommits / ZNUMA::count(), vmem.size()), ZGranuleSize);
224+
}
225+
217226
size_t ZPhysicalMemoryManager::commit(const ZVirtualMemory& vmem, uint32_t numa_id) {
218227
zbacking_index* const pmem = _physical_mappings.addr(vmem.start());
219-
const size_t size = vmem.size();
228+
const size_t size = ZFailLargerCommits > 0
229+
? inject_commit_limit(vmem)
230+
: vmem.size();
220231

221232
size_t total_committed = 0;
222233

src/hotspot/share/gc/z/z_globals.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@
118118
develop(bool, ZVerifyOops, false, \
119119
"Verify accessed oops") \
120120
\
121+
develop(size_t, ZFailLargerCommits, 0, \
122+
"Commits larger than ZFailLargerCommits will be truncated, " \
123+
"used to stress page allocation commit failure paths " \
124+
"(0: Disabled)") \
125+
\
121126
develop(uint, ZFakeNUMA, 1, \
122127
"ZFakeNUMA is used to test the internal NUMA memory support " \
123128
"without the need for UseNUMA") \
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright (c) 2025, 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+
package gc.z;
25+
26+
/*
27+
* @test id=ZFakeNUMA
28+
* @requires vm.gc.Z & vm.debug
29+
* @library / /test/lib
30+
* @summary Test ZGC graceful failure when a commit fails (with ZFakeNUMA)
31+
* @run driver gc.z.TestCommitFailure -XX:ZFakeNUMA=16
32+
*/
33+
34+
import jdk.test.lib.process.ProcessTools;
35+
36+
import static gc.testlibrary.Allocation.blackHole;
37+
38+
import java.util.ArrayList;
39+
import java.util.Arrays;
40+
import java.util.List;
41+
42+
public class TestCommitFailure {
43+
static final int K = 1024;
44+
static final int M = 1024 * K;
45+
46+
static final int XMS = 128 * M;
47+
static final int XMX = 512 * M;
48+
49+
static class Test {
50+
static final int LARGE_ALLOC = 256 * M;
51+
static final int SMALL_GARBAGE = 256 * M;
52+
static final int SMALL_LIVE = 128 * M;
53+
54+
// Allocates at least totalLive bytes of objects and add them to list.
55+
static void allocLive(List<Object> list, int totalLive) {
56+
final int largePageAllocationSize = 6 * M;
57+
for (int live = 0; live < totalLive; live += largePageAllocationSize) {
58+
list.add(new byte[largePageAllocationSize - K]);
59+
}
60+
}
61+
62+
// Allocates at least totalGarbage bytes of garbage large pages.
63+
static void allocGarbage(int totalGarbage) {
64+
final int largePageAllocationSize = 6 * M;
65+
for (int garbage = 0; garbage < totalGarbage; garbage += largePageAllocationSize) {
66+
blackHole(new byte[largePageAllocationSize - K]);
67+
}
68+
}
69+
70+
public static void main(String[] args) {
71+
final var list = new ArrayList<Object>();
72+
try {
73+
// Fill heap with small live objects
74+
allocLive(list, SMALL_LIVE);
75+
// Fill with small garbage objects
76+
allocGarbage(SMALL_GARBAGE);
77+
// Allocate large objects where commit fails until an OOME is thrown
78+
while (true) {
79+
list.add(new byte[LARGE_ALLOC - K]);
80+
}
81+
} catch (OutOfMemoryError oome) {}
82+
blackHole(list);
83+
}
84+
}
85+
86+
public static void main(String[] args) throws Exception {
87+
final int xmxInM = XMX / M;
88+
final int xmsInM = XMS / M;
89+
final var arguments = new ArrayList(Arrays.asList(args));
90+
arguments.addAll(List.of(
91+
"-XX:+UseZGC",
92+
"-Xlog:gc+init",
93+
"-XX:ZFailLargerCommits=" + XMS,
94+
"-Xms" + xmsInM + "M",
95+
"-Xmx" + xmxInM + "M",
96+
Test.class.getName()));
97+
98+
ProcessTools.executeTestJava(arguments)
99+
.outputTo(System.out)
100+
.errorTo(System.out)
101+
.shouldHaveExitValue(0);
102+
}
103+
}

0 commit comments

Comments
 (0)