2023-04-02 06:21:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022 SAP SE. All rights reserved.
|
2024-06-24 13:33:20 +00:00
|
|
|
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
|
2023-04-02 06:21:30 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import jdk.test.lib.process.OutputAnalyzer;
|
|
|
|
import jdk.test.lib.process.ProcessTools;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
2024-06-17 06:16:26 +00:00
|
|
|
import java.util.Collections;
|
2023-04-02 06:21:30 +00:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2023-05-11 04:50:37 +00:00
|
|
|
import java.util.concurrent.CyclicBarrier;
|
2023-04-02 06:21:30 +00:00
|
|
|
|
2024-06-24 13:33:20 +00:00
|
|
|
import static jdk.test.lib.Platform.isLinux;
|
|
|
|
import static jdk.test.lib.Platform.isWindows;
|
|
|
|
|
2023-04-02 06:21:30 +00:00
|
|
|
/*
|
2024-06-24 13:33:20 +00:00
|
|
|
* @test id=preTouch
|
2023-04-02 06:21:30 +00:00
|
|
|
* @summary Test AlwaysPreTouchThreadStacks
|
2023-07-24 15:35:22 +00:00
|
|
|
* @requires os.family != "aix"
|
2023-04-02 06:21:30 +00:00
|
|
|
* @library /test/lib
|
|
|
|
* @modules java.base/jdk.internal.misc
|
|
|
|
* java.management
|
2024-06-24 13:33:20 +00:00
|
|
|
* @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
|
2023-04-02 06:21:30 +00:00
|
|
|
*/
|
|
|
|
|
2023-05-11 04:50:37 +00:00
|
|
|
public class TestAlwaysPreTouchStacks {
|
2023-04-02 06:21:30 +00:00
|
|
|
|
2023-05-11 04:50:37 +00:00
|
|
|
// 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;
|
|
|
|
static int memoryCeilingMB = 128;
|
|
|
|
static int threadStackSizeMB = 8;
|
|
|
|
static int numThreads = memoryCeilingMB / threadStackSizeMB;
|
|
|
|
static CyclicBarrier gate = new CyclicBarrier(numThreads + 1);
|
|
|
|
|
|
|
|
static private final Thread createTestThread(int num) {
|
2023-04-02 06:21:30 +00:00
|
|
|
Thread t = new Thread(null,
|
2023-05-11 04:50:37 +00:00
|
|
|
() -> {
|
|
|
|
System.out.println("Alive: " + num);
|
|
|
|
try {
|
|
|
|
// report aliveness, then sleep until VM death
|
|
|
|
gate.await();
|
|
|
|
for (;;) {
|
|
|
|
Thread.sleep(1000);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"TestThread-" + num, threadStackSizeMB * MB);
|
|
|
|
t.setDaemon(true);
|
2023-04-02 06:21:30 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2023-05-11 04:50:37 +00:00
|
|
|
public static void main(String[] args) throws Exception {
|
2023-04-02 06:21:30 +00:00
|
|
|
|
|
|
|
if (args.length == 1 && args[0].equals("test")) {
|
|
|
|
|
|
|
|
ArrayList<Thread> threads = new ArrayList<>();
|
|
|
|
|
2023-05-11 04:50:37 +00:00
|
|
|
// Add a bunch of large-stacked threads to make a significant imprint on combined thread stack size
|
|
|
|
for (int i = 0; i < numThreads; i++) {
|
|
|
|
threads.add(createTestThread(i));
|
2023-04-02 06:21:30 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 04:50:37 +00:00
|
|
|
// Start test threads.
|
2023-04-02 06:21:30 +00:00
|
|
|
threads.forEach(Thread::start);
|
2023-05-11 04:50:37 +00:00
|
|
|
|
|
|
|
gate.await();
|
|
|
|
|
|
|
|
// Stop VM. VM will run PrintNMTStatistics before exiting, and the still-running daemon threads
|
|
|
|
// should show up with fully - or almost fully - committed thread stacks.
|
2023-04-02 06:21:30 +00:00
|
|
|
|
|
|
|
} else {
|
2024-06-24 13:33:20 +00:00
|
|
|
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'.");
|
|
|
|
}
|
2024-06-17 06:16:26 +00:00
|
|
|
ArrayList<String> vmArgs = new ArrayList<>();
|
|
|
|
Collections.addAll(vmArgs,
|
2023-04-02 06:21:30 +00:00
|
|
|
"-XX:+UnlockDiagnosticVMOptions",
|
|
|
|
"-Xmx100M",
|
2024-06-17 06:16:26 +00:00
|
|
|
"-XX:NativeMemoryTracking=summary", "-XX:+PrintNMTStatistics");
|
2024-06-24 13:33:20 +00:00
|
|
|
if (preTouch){
|
|
|
|
vmArgs.add("-XX:+AlwaysPreTouchStacks");
|
|
|
|
}
|
2024-06-17 06:16:26 +00:00
|
|
|
if (System.getProperty("os.name").contains("Linux")) {
|
|
|
|
vmArgs.add("-XX:-UseMadvPopulateWrite");
|
|
|
|
}
|
|
|
|
Collections.addAll(vmArgs, "TestAlwaysPreTouchStacks", "test");
|
|
|
|
ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(vmArgs);
|
2023-04-02 06:21:30 +00:00
|
|
|
OutputAnalyzer output = new OutputAnalyzer(pb.start());
|
|
|
|
output.reportDiagnosticSummary();
|
|
|
|
|
|
|
|
output.shouldHaveExitValue(0);
|
|
|
|
|
2023-05-11 04:50:37 +00:00
|
|
|
for (int i = 0; i < numThreads; i++) {
|
|
|
|
output.shouldContain("Alive: " + i);
|
2023-04-02 06:21:30 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 13:33:20 +00:00
|
|
|
// 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:
|
2023-04-02 06:21:30 +00:00
|
|
|
// - 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);
|
2023-05-11 04:50:37 +00:00
|
|
|
// 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.
|
2024-06-24 13:33:20 +00:00
|
|
|
if (preTouch && (double)committed < ((double)reserved * 0.75)) {
|
2023-04-02 06:21:30 +00:00
|
|
|
throw new RuntimeException("Expected a higher ratio between stack committed and reserved.");
|
2024-06-24 13:33:20 +00:00
|
|
|
} else if (!preTouch && (double)committed > ((double)reserved * 0.50)){
|
|
|
|
throw new RuntimeException("Expected a lower ratio between stack committed and reserved.");
|
2023-04-02 06:21:30 +00:00
|
|
|
}
|
2023-05-11 04:50:37 +00:00
|
|
|
// 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 + ")");
|
|
|
|
}
|
2023-04-02 06:21:30 +00:00
|
|
|
foundLine = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!foundLine) {
|
|
|
|
throw new RuntimeException("Did not find expected NMT output");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|