Skip to content

Commit cefd945

Browse files
tejalwakchaureshipilev
authored andcommitted
8365919: Replace currentTimeMillis with nanoTime in Stresser.java
Backport-of: 0ca38bdc4d503158fda57bbc8bc9adc420628079
1 parent 147ed00 commit cefd945

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

test/hotspot/jtreg/vmTestbase/nsk/share/test/Stresser.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@ public class Stresser implements ExecutionController {
5858
private String name;
5959
private long maxIterations;
6060
private long iterations;
61+
62+
// In nanoseconds
6163
private long startTime;
62-
private long finishTime;
6364
private long currentTime;
65+
private long stressTime;
66+
6467
private PrintStream defaultOutput = System.out;
6568

6669
/*
@@ -179,15 +182,15 @@ public void printStressInfo(PrintStream out) {
179182
*/
180183
public void printExecutionInfo(PrintStream out) {
181184
println(out, "Completed iterations: " + iterations);
182-
println(out, "Execution time: " + (currentTime - startTime) + " seconds");
185+
println(out, "Execution time: " + (currentTime - startTime) / 1_000_000_000.0 + " seconds");
183186
if (!finished) {
184187
println(out, "Execution is not finished yet");
185188
} else if (forceFinish) {
186189
println(out, "Execution was forced to finish");
187190
} else if (maxIterations != 0 && iterations >= maxIterations) {
188191
println(out, "Execution finished because number of iterations was exceeded: " + iterations + " >= " + maxIterations);
189-
} else if (finishTime != 0 && currentTime >= finishTime) {
190-
println(out, "Execution finished because time was exceeded: " + (currentTime - startTime) + " >= " + (finishTime - startTime));
192+
} else if (stressTime != 0 && (currentTime - startTime) >= stressTime) {
193+
println(out, "Execution finished because time has exceeded stress time: " + stressTime / 1_000_000_000 + " seconds");
191194
}
192195
}
193196

@@ -208,13 +211,8 @@ private void println(PrintStream out, String s) {
208211
public void start(long stdIterations) {
209212
maxIterations = stdIterations * options.getIterationsFactor();
210213
iterations = 0;
211-
long stressTime = options.getTime();
212-
startTime = System.currentTimeMillis();
213-
if (stressTime == 0) {
214-
finishTime = 0;
215-
} else {
216-
finishTime = startTime + stressTime * 1000;
217-
}
214+
stressTime = options.getTime() * 1_000_000_000;
215+
startTime = System.nanoTime();
218216
finished = false;
219217
forceFinish = false;
220218
if (options.isDebugEnabled()) {
@@ -232,7 +230,7 @@ public void start(long stdIterations) {
232230
* finally {} block.
233231
*/
234232
public void finish() {
235-
currentTime = System.currentTimeMillis();
233+
currentTime = System.nanoTime();
236234
finished = true;
237235
if (options.isDebugEnabled()) {
238236
printExecutionInfo(defaultOutput);
@@ -255,10 +253,12 @@ public void forceFinish() {
255253
*/
256254
public boolean iteration() {
257255
++iterations;
256+
boolean result = continueExecution();
257+
// Call print at the end to show the most up-to-date info.
258258
if (options.isDebugDetailed()) {
259259
printExecutionInfo(defaultOutput);
260260
}
261-
return continueExecution();
261+
return result;
262262
}
263263

264264
/**
@@ -267,14 +267,14 @@ public boolean iteration() {
267267
* @return true if execution needs to continue
268268
*/
269269
public boolean continueExecution() {
270-
currentTime = System.currentTimeMillis();
270+
currentTime = System.nanoTime();
271271
if (startTime == 0) {
272272
throw new TestBug("Stresser is not started.");
273273
}
274274
return !forceFinish
275275
&& !finished
276276
&& (maxIterations == 0 || iterations < maxIterations)
277-
&& (finishTime == 0 || currentTime < finishTime);
277+
&& (stressTime == 0 || (currentTime - startTime) < stressTime);
278278
}
279279

280280
/**
@@ -309,7 +309,7 @@ public long getIterationsLeft() {
309309
* @return time
310310
*/
311311
public long getExecutionTime() {
312-
return System.currentTimeMillis() - startTime;
312+
return (System.nanoTime() - startTime) / 1_000_000;
313313
}
314314

315315
/**
@@ -318,11 +318,11 @@ public long getExecutionTime() {
318318
* @return time
319319
*/
320320
public long getTimeLeft() {
321-
long current = System.currentTimeMillis();
322-
if (current >= finishTime) {
321+
long elapsedTime = System.nanoTime() - startTime;
322+
if (elapsedTime >= stressTime) {
323323
return 0;
324324
} else {
325-
return finishTime - current;
325+
return (stressTime - elapsedTime) / 1_000_000;
326326
}
327327
}
328328

0 commit comments

Comments
 (0)