From 7af0923b01e48b44f7f49aeacbc5a28d82c8975f Mon Sep 17 00:00:00 2001 From: Albert Yang Date: Thu, 21 Aug 2025 11:48:06 +0200 Subject: [PATCH 1/2] test-nano-time --- .../vmTestbase/nsk/share/test/Stresser.java | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/test/hotspot/jtreg/vmTestbase/nsk/share/test/Stresser.java b/test/hotspot/jtreg/vmTestbase/nsk/share/test/Stresser.java index 83e8baa283699..98f18bc2f1519 100644 --- a/test/hotspot/jtreg/vmTestbase/nsk/share/test/Stresser.java +++ b/test/hotspot/jtreg/vmTestbase/nsk/share/test/Stresser.java @@ -58,9 +58,11 @@ public class Stresser implements ExecutionController { private String name; private long maxIterations; private long iterations; + + // In milliseconds private long startTime; - private long finishTime; private long currentTime; + private PrintStream defaultOutput = System.out; /* @@ -179,15 +181,15 @@ public void printStressInfo(PrintStream out) { */ public void printExecutionInfo(PrintStream out) { println(out, "Completed iterations: " + iterations); - println(out, "Execution time: " + (currentTime - startTime) + " seconds"); + println(out, "Execution time: " + (currentTime - startTime)/1000.0 + " seconds"); if (!finished) { println(out, "Execution is not finished yet"); } else if (forceFinish) { println(out, "Execution was forced to finish"); } else if (maxIterations != 0 && iterations >= maxIterations) { println(out, "Execution finished because number of iterations was exceeded: " + iterations + " >= " + maxIterations); - } else if (finishTime != 0 && currentTime >= finishTime) { - println(out, "Execution finished because time was exceeded: " + (currentTime - startTime) + " >= " + (finishTime - startTime)); + } else if (options.getTime() != 0 && (currentTime - startTime) >= options.getTime() * 1000) { + println(out, "Execution finished because time was exceeded: " + (currentTime - startTime) + " >= " + options.getTime() * 1000); } } @@ -209,12 +211,7 @@ public void start(long stdIterations) { maxIterations = stdIterations * options.getIterationsFactor(); iterations = 0; long stressTime = options.getTime(); - startTime = System.currentTimeMillis(); - if (stressTime == 0) { - finishTime = 0; - } else { - finishTime = startTime + stressTime * 1000; - } + startTime = System.nanoTime()/1000000; finished = false; forceFinish = false; if (options.isDebugEnabled()) { @@ -232,7 +229,7 @@ public void start(long stdIterations) { * finally {} block. */ public void finish() { - currentTime = System.currentTimeMillis(); + currentTime = System.nanoTime()/1000000; finished = true; if (options.isDebugEnabled()) { printExecutionInfo(defaultOutput); @@ -255,10 +252,12 @@ public void forceFinish() { */ public boolean iteration() { ++iterations; + boolean result = continueExecution(); + // Call print at the end to show the most up-to-date info. if (options.isDebugDetailed()) { printExecutionInfo(defaultOutput); } - return continueExecution(); + return result; } /** @@ -267,14 +266,14 @@ public boolean iteration() { * @return true if execution needs to continue */ public boolean continueExecution() { - currentTime = System.currentTimeMillis(); + currentTime = System.nanoTime()/1000000; if (startTime == 0) { throw new TestBug("Stresser is not started."); } return !forceFinish && !finished && (maxIterations == 0 || iterations < maxIterations) - && (finishTime == 0 || currentTime < finishTime); + && (options.getTime() == 0 || (currentTime - startTime) < options.getTime() * 1000); } /** @@ -309,7 +308,7 @@ public long getIterationsLeft() { * @return time */ public long getExecutionTime() { - return System.currentTimeMillis() - startTime; + return System.nanoTime()/1000000 - startTime; } /** @@ -318,11 +317,11 @@ public long getExecutionTime() { * @return time */ public long getTimeLeft() { - long current = System.currentTimeMillis(); - if (current >= finishTime) { + long elapsedTime = System.nanoTime()/1000000 - startTime; + if (elapsedTime >= options.getTime() * 1000) { return 0; } else { - return finishTime - current; + return options.getTime() * 1000 - elapsedTime; } } From cefb9948e6770fd4cf81de001f072c3e0a4f0f5f Mon Sep 17 00:00:00 2001 From: Albert Yang Date: Fri, 22 Aug 2025 20:33:49 +0200 Subject: [PATCH 2/2] review --- test/hotspot/jtreg/vmTestbase/nsk/share/test/Stresser.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/hotspot/jtreg/vmTestbase/nsk/share/test/Stresser.java b/test/hotspot/jtreg/vmTestbase/nsk/share/test/Stresser.java index 98f18bc2f1519..4ce233a697a67 100644 --- a/test/hotspot/jtreg/vmTestbase/nsk/share/test/Stresser.java +++ b/test/hotspot/jtreg/vmTestbase/nsk/share/test/Stresser.java @@ -318,10 +318,11 @@ public long getExecutionTime() { */ public long getTimeLeft() { long elapsedTime = System.nanoTime()/1000000 - startTime; - if (elapsedTime >= options.getTime() * 1000) { + long stressTime = options.getTime() * 1000; + if (elapsedTime >= stressTime) { return 0; } else { - return options.getTime() * 1000 - elapsedTime; + return stressTime - elapsedTime; } }