8335167: Test runtime/Thread/TestAlwaysPreTouchStacks.java failed with Expected a higher ratio between stack committed and reserved

Reviewed-by: stuefe, dholmes, gziemski
This commit is contained in:
Afshin Zafari 2024-09-24 07:56:14 +00:00
parent 9176f6810e
commit e60e882156
2 changed files with 88 additions and 90 deletions

View File

@ -116,7 +116,6 @@ runtime/os/TestTracePageSizes.java#Serial 8267460 linux-aarch64
runtime/ErrorHandling/CreateCoredumpOnCrash.java 8267433 macosx-x64
runtime/StackGuardPages/TestStackGuardPagesNative.java 8303612 linux-all
runtime/ErrorHandling/MachCodeFramesInErrorFile.java 8313315 linux-ppc64le
runtime/Thread/TestAlwaysPreTouchStacks.java 8335167 macosx-aarch64
runtime/cds/appcds/customLoader/HelloCustom_JFR.java 8241075 linux-all,windows-x64
applications/jcstress/copy.java 8229852 linux-all

View File

@ -36,7 +36,7 @@ import static jdk.test.lib.Platform.isLinux;
import static jdk.test.lib.Platform.isWindows;
/*
* @test id=preTouch
* @test id=preTouchTest
* @summary Test AlwaysPreTouchThreadStacks
* @requires os.family != "aix"
* @library /test/lib
@ -45,23 +45,16 @@ import static jdk.test.lib.Platform.isWindows;
* @run driver TestAlwaysPreTouchStacks preTouch
*/
/*
* @test id=noPreTouch
* @summary Test that only touched committed memory is reported as thread stack usage.
* @requires os.family != "aix"
* @library /test/lib
* @modules java.base/jdk.internal.misc
* java.management
* @run driver TestAlwaysPreTouchStacks noPreTouch
*/
public class TestAlwaysPreTouchStacks {
// We will create a bunch of large-stacked threads to make a significant imprint on combined thread stack size
final static int MB = 1024*1024;
final static int MB = 1024 * 1024;
static int memoryCeilingMB = 128;
static int threadStackSizeMB = 8;
static int numThreads = memoryCeilingMB / threadStackSizeMB;
static long min_stack_usage_with_pretouch = 1 * MB;
static long max_stack_usage_with_pretouch = (long)(0.75 * threadStackSizeMB * MB);
static CyclicBarrier gate = new CyclicBarrier(numThreads + 1);
static private final Thread createTestThread(int num) {
@ -79,10 +72,76 @@ public class TestAlwaysPreTouchStacks {
}
},
"TestThread-" + num, threadStackSizeMB * MB);
// Let test threads run as daemons to ensure that they are still running and
// that their stacks are still allocated when the JVM shuts down and the final
// NMT report is printed.
t.setDaemon(true);
return t;
}
private static long runPreTouchTest(boolean preTouch) throws Exception {
long reserved = 0L, committed = 0L;
ArrayList<String> vmArgs = new ArrayList<>();
Collections.addAll(vmArgs,
"-XX:+UnlockDiagnosticVMOptions",
"-Xmx100M",
"-XX:NativeMemoryTracking=summary", "-XX:+PrintNMTStatistics");
if (preTouch){
vmArgs.add("-XX:+AlwaysPreTouchStacks");
}
if (System.getProperty("os.name").contains("Linux")) {
vmArgs.add("-XX:-UseMadvPopulateWrite");
}
Collections.addAll(vmArgs, "TestAlwaysPreTouchStacks", "test");
ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(vmArgs);
OutputAnalyzer output = new OutputAnalyzer(pb.start());
output.shouldHaveExitValue(0);
for (int i = 0; i < numThreads; i++) {
output.shouldContain("Alive: " + i);
}
// If using -XX:+AlwaysPreTouchStacks, we want to see, in the final NMT printout,
// a committed thread stack size very close to reserved stack size. Like this:
// - Thread (reserved=10332400KB, committed=10284360KB)
// (thread #10021)
// (stack: reserved=10301560KB, committed=10253520KB) <<<<
//
// ... without -XX:+AlwaysPreTouchStacks, the committed/reserved ratio for thread stacks should be
// a lot lower, e.g.:
// - Thread (reserved=10332400KB, committed=331828KB)
// (thread #10021)
// (stack: reserved=10301560KB, committed=300988KB) <<<
output.shouldMatch("- *Thread.*reserved.*committed");
output.reportDiagnosticSummary();
Pattern pat = Pattern.compile(".*stack: reserved=(\\d+), committed=(\\d+).*");
boolean foundLine = false;
for (String line : output.asLines()) {
Matcher m = pat.matcher(line);
if (m.matches()) {
reserved = Long.parseLong(m.group(1));
committed = Long.parseLong(m.group(2));
System.out.println(">>>>> " + line + ": " + reserved + " - " + committed);
// Added sanity tests: we expect our test threads to be still alive when NMT prints its final
// report, so their stacks should dominate the NMT-reported total stack size.
long max_reserved = memoryCeilingMB * 3 * MB;
long min_reserved = memoryCeilingMB * MB;
if (reserved >= max_reserved || reserved < min_reserved) {
throw new RuntimeException("Total reserved stack sizes outside of our expectations (" + reserved +
", expected " + min_reserved + ".." + max_reserved + ")");
}
foundLine = true;
break;
}
}
if (!foundLine) {
throw new RuntimeException("Did not find expected NMT output");
}
return committed;
}
public static void main(String[] args) throws Exception {
if (args.length == 1 && args[0].equals("test")) {
@ -103,83 +162,23 @@ public class TestAlwaysPreTouchStacks {
// should show up with fully - or almost fully - committed thread stacks.
} else {
boolean preTouch;
if (args.length == 1 && args[0].equals("noPreTouch")){
preTouch = false;
} else if (args.length == 1 && args[0].equals("preTouch")){
preTouch = true;
} else {
throw new RuntimeException("Invalid test input. Must be 'preTouch' or 'noPreTouch'.");
}
ArrayList<String> vmArgs = new ArrayList<>();
Collections.addAll(vmArgs,
"-XX:+UnlockDiagnosticVMOptions",
"-Xmx100M",
"-XX:NativeMemoryTracking=summary", "-XX:+PrintNMTStatistics");
if (preTouch){
vmArgs.add("-XX:+AlwaysPreTouchStacks");
}
if (System.getProperty("os.name").contains("Linux")) {
vmArgs.add("-XX:-UseMadvPopulateWrite");
}
Collections.addAll(vmArgs, "TestAlwaysPreTouchStacks", "test");
ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(vmArgs);
OutputAnalyzer output = new OutputAnalyzer(pb.start());
output.reportDiagnosticSummary();
output.shouldHaveExitValue(0);
for (int i = 0; i < numThreads; i++) {
output.shouldContain("Alive: " + i);
}
// If using -XX:+AlwaysPreTouchStacks, we want to see, in the final NMT printout,
// a committed thread stack size very close to reserved stack size. Like this:
// - Thread (reserved=10332400KB, committed=10284360KB)
// (thread #10021)
// (stack: reserved=10301560KB, committed=10253520KB) <<<<
//
// ... without -XX:+AlwaysPreTouchStacks, the committed/reserved ratio for thread stacks should be
// a lot lower, e.g.:
// - Thread (reserved=10332400KB, committed=331828KB)
// (thread #10021)
// (stack: reserved=10301560KB, committed=300988KB) <<<
output.shouldMatch("- *Thread.*reserved.*committed");
Pattern pat = Pattern.compile(".*stack: reserved=(\\d+), committed=(\\d+).*");
boolean foundLine = false;
for (String line : output.asLines()) {
Matcher m = pat.matcher(line);
if (m.matches()) {
long reserved = Long.parseLong(m.group(1));
long committed = Long.parseLong(m.group(2));
System.out.println(">>>>> " + line + ": " + reserved + " - " + committed);
// This is a bit fuzzy: even with PreTouch we don't commit the full range of what NMT counts
// as thread stack. But without pre-touching, the thread stacks would be committed to about 1/5th
// of their reserved size. Requiring them to be committed for over 3/4th shows that pretouch is
// really working.
if (preTouch && (double)committed < ((double)reserved * 0.75)) {
throw new RuntimeException("Expected a higher ratio between stack committed and reserved.");
} else if (!preTouch && (double)committed > ((double)reserved * 0.50)){
throw new RuntimeException("Expected a lower ratio between stack committed and reserved.");
}
// Added sanity tests: we expect our test threads to be still alive when NMT prints its final
// report, so their stacks should dominate the NMT-reported total stack size.
long max_reserved = memoryCeilingMB * 3 * MB;
long min_reserved = memoryCeilingMB * MB;
if (reserved >= max_reserved || reserved < min_reserved) {
throw new RuntimeException("Total reserved stack sizes outside of our expectations (" + reserved +
", expected " + min_reserved + ".." + max_reserved + ")");
}
foundLine = true;
break;
}
}
if (!foundLine) {
throw new RuntimeException("Did not find expected NMT output");
}
}
long pretouch_committed = runPreTouchTest(true);
long no_pretouch_committed = runPreTouchTest(false);
if (pretouch_committed == 0 || no_pretouch_committed == 0) {
throw new RuntimeException("Could not run with PreTouch flag.");
}
long expected_delta = numThreads * (max_stack_usage_with_pretouch - min_stack_usage_with_pretouch);
long actual_delta = pretouch_committed - no_pretouch_committed;
if (pretouch_committed <= (no_pretouch_committed + expected_delta)) {
throw new RuntimeException("Expected a higher amount of committed with pretouch stacks" +
"PreTouch amount: " + pretouch_committed +
"NoPreTouch amount: " + (no_pretouch_committed + expected_delta));
}
if (actual_delta < expected_delta) {
throw new RuntimeException("Expected a higher delta between stack committed of with and without pretouch." +
"Expected: " + expected_delta + " Actual: " + actual_delta);
}
}
}
}