Skip to content

Commit 17a19dc

Browse files
committed
8311639: Replace currentTimeMillis() with nanoTime() in jtreg/gc
Reviewed-by: stefank, ayang
1 parent 0b3f452 commit 17a19dc

17 files changed

+112
-125
lines changed

test/hotspot/jtreg/gc/cslocker/TestCSLocker.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -44,10 +44,8 @@
4444

4545
public class TestCSLocker extends Thread
4646
{
47-
static int timeout = 5000;
47+
static int timeoutMillis = 5000;
4848
public static void main(String args[]) throws Exception {
49-
long startTime = System.currentTimeMillis();
50-
5149
// start garbage producer thread
5250
GarbageProducer garbageProducer = new GarbageProducer(1000000, 10);
5351
garbageProducer.start();
@@ -61,9 +59,7 @@ public static void main(String args[]) throws Exception {
6159
// code until unlock() is called below.
6260

6361
// check timeout to success deadlocking
64-
while (System.currentTimeMillis() < startTime + timeout) {
65-
sleep(1000);
66-
}
62+
sleep(timeoutMillis);
6763

6864
csLocker.unlock();
6965
garbageProducer.interrupt();

test/hotspot/jtreg/gc/g1/TestEagerReclaimHumongousRegionsClearMarkBits.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -54,7 +54,7 @@ class ObjectWithSomeRefs {
5454
}
5555

5656
class TestEagerReclaimHumongousRegionsClearMarkBitsReclaimRegionFast {
57-
public static final long MAX_MILLIS_FOR_RUN = 50 * 1000; // The maximum runtime for the actual test.
57+
public static final long MAX_NANOS_FOR_RUN = 50L * 1_000_000_000L; // The maximum runtime for the actual test.
5858

5959
public static final int M = 1024*1024;
6060

@@ -93,11 +93,11 @@ public static void main(String[] args) {
9393

9494
Object ref_from_stack = large1;
9595

96-
long start_millis = System.currentTimeMillis();
96+
long start_nanos = System.nanoTime();
9797

9898
for (int i = 0; i < 20; i++) {
99-
long current_millis = System.currentTimeMillis();
100-
if ((current_millis - start_millis) > MAX_MILLIS_FOR_RUN) {
99+
long current_nanos = System.nanoTime();
100+
if ((current_nanos - start_nanos) > MAX_NANOS_FOR_RUN) {
101101
System.out.println("Finishing test because maximum runtime exceeded");
102102
break;
103103
}

test/hotspot/jtreg/gc/g1/TestPeriodicCollectionJNI.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -57,19 +57,15 @@ public static void block() {
5757
}
5858

5959
public static void main(String[] args) throws InterruptedException {
60-
long timeout = 2000;
61-
long startTime = System.currentTimeMillis();
60+
long timeoutMillis = 2000;
6261

6362
// Start thread doing JNI call
6463
BlockInNative blocker = new BlockInNative();
6564
blocker.start();
6665

6766
try {
6867
// Wait for periodic GC timeout to trigger
69-
while (System.currentTimeMillis() < startTime + timeout) {
70-
System.out.println("Sleeping to let periodic GC trigger...");
71-
Thread.sleep(200);
72-
}
68+
Thread.sleep(timeoutMillis);
7369
} finally {
7470
unblock();
7571
}

test/hotspot/jtreg/gc/g1/humongousObjects/TestNoAllocationsInHRegions.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -178,11 +178,11 @@ public static void main(String[] args) {
178178
}
179179

180180
// test duration
181-
long duration = Integer.parseInt(args[0]) * 1000L;
181+
long durationNanos = Integer.parseInt(args[0]) * 1_000_000_000L;
182182
// part of heap preallocated with humongous objects (in percents)
183183
int percentOfAllocatedHeap = Integer.parseInt(args[1]);
184184

185-
long startTime = System.currentTimeMillis();
185+
long startTimeNanos = System.nanoTime();
186186

187187
long initialFreeRegionsCount = WB.g1NumFreeRegions();
188188
int regionsToAllocate = (int) ((double) initialFreeRegionsCount / 100.0 * percentOfAllocatedHeap);
@@ -219,7 +219,7 @@ public static void main(String[] args) {
219219

220220
threads.stream().forEach(Thread::start);
221221

222-
while ((System.currentTimeMillis() - startTime < duration) && error == null) {
222+
while ((System.nanoTime() - startTimeNanos < durationNanos) && error == null) {
223223
Thread.yield();
224224
}
225225

test/hotspot/jtreg/gc/logging/TestUnifiedLoggingSwitchStress.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -222,9 +222,9 @@ public static void main(String[] args) throws InterruptedException {
222222
if (args.length != 1) {
223223
throw new Error("Test Bug: Expected duration (in seconds) wasn't provided as command line argument");
224224
}
225-
long duration = Integer.parseInt(args[0]) * 1000;
225+
long durationNanos = Integer.parseInt(args[0]) * 1_000_000_000L;
226226

227-
long startTime = System.currentTimeMillis();
227+
long startTimeNanos = System.nanoTime();
228228

229229
List<Thread> threads = new LinkedList<>();
230230

@@ -238,7 +238,7 @@ public static void main(String[] args) throws InterruptedException {
238238

239239
threads.stream().forEach(Thread::start);
240240

241-
while (System.currentTimeMillis() - startTime < duration) {
241+
while (System.nanoTime() - startTimeNanos < durationNanos) {
242242
Thread.yield();
243243
}
244244

test/hotspot/jtreg/gc/shenandoah/TestStringDedupStress.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public class TestStringDedupStress {
113113

114114
private static final int TARGET_STRINGS = Integer.getInteger("targetStrings", 2_500_000);
115115
private static final long MAX_REWRITE_GC_CYCLES = 6;
116-
private static final long MAX_REWRITE_TIME = 30*1000; // ms
116+
private static final long MAX_REWRITE_TIME_NS = 30L * 1_000_000_000L; // 30s in ns
117117

118118
private static final int UNIQUE_STRINGS = 20;
119119

@@ -211,7 +211,7 @@ public static void main(String[] args) {
211211
}
212212

213213
long cycleBeforeRewrite = gcCycleMBean.getCollectionCount();
214-
long timeBeforeRewrite = System.currentTimeMillis();
214+
long timeBeforeRewriteNanos = System.nanoTime();
215215

216216
long loop = 1;
217217
while (true) {
@@ -229,7 +229,7 @@ public static void main(String[] args) {
229229
}
230230

231231
// enough time is spent waiting for GC to happen
232-
if (System.currentTimeMillis() - timeBeforeRewrite >= MAX_REWRITE_TIME) {
232+
if (System.nanoTime() - timeBeforeRewriteNanos >= MAX_REWRITE_TIME_NS) {
233233
break;
234234
}
235235
}

test/hotspot/jtreg/gc/shenandoah/jni/TestJNIGlobalRefs.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class TestJNIGlobalRefs {
4949
System.loadLibrary("TestJNIGlobalRefs");
5050
}
5151

52-
private static final int TIME_MSEC = 120000;
52+
private static final long TIME_NSEC = 120L * 1_000_000_000L;
5353
private static final int ARRAY_SIZE = 10000;
5454

5555
private static native void makeGlobalRef(Object o);
@@ -60,13 +60,13 @@ public class TestJNIGlobalRefs {
6060
public static void main(String[] args) throws Throwable {
6161
seedGlobalRef();
6262
seedWeakGlobalRef();
63-
long start = System.currentTimeMillis();
64-
long current = start;
65-
while (current - start < TIME_MSEC) {
63+
long startNanos = System.nanoTime();
64+
long currentNanos = startNanos;
65+
while (currentNanos - startNanos < TIME_NSEC) {
6666
testGlobal();
6767
testWeakGlobal();
6868
Thread.sleep(1);
69-
current = System.currentTimeMillis();
69+
currentNanos = System.nanoTime();
7070
}
7171
}
7272

test/hotspot/jtreg/gc/shenandoah/mxbeans/TestChurnNotifications.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public class TestChurnNotifications {
129129
static volatile Object sink;
130130

131131
public static void main(String[] args) throws Exception {
132-
final long startTime = System.currentTimeMillis();
132+
final long startTimeNanos = System.nanoTime();
133133

134134
final AtomicLong churnBytes = new AtomicLong();
135135

@@ -176,8 +176,8 @@ public void handleNotification(Notification n, Object o) {
176176
// Look at test timeout to figure out how long we can wait without breaking into timeout.
177177
// Default to 1/4 of the remaining time in 1s steps.
178178
final long STEP_MS = 1000;
179-
long spentTime = System.currentTimeMillis() - startTime;
180-
long maxTries = (Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT) - spentTime) / STEP_MS / 4;
179+
long spentTimeNanos = System.nanoTime() - startTimeNanos;
180+
long maxTries = (Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT) - (spentTimeNanos / 1_000_000L)) / STEP_MS / 4;
181181

182182
// Wait until enough notifications are accrued to match minimum boundary.
183183
long tries = 0;

test/hotspot/jtreg/gc/shenandoah/mxbeans/TestPauseNotifications.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private static boolean isExpectedPauseAction(String action) {
122122
}
123123

124124
public static void main(String[] args) throws Exception {
125-
final long startTime = System.currentTimeMillis();
125+
final long startTimeNanos = System.nanoTime();
126126

127127
final AtomicLong pausesDuration = new AtomicLong();
128128
final AtomicLong cyclesDuration = new AtomicLong();
@@ -173,8 +173,8 @@ public void handleNotification(Notification n, Object o) {
173173
// Look at test timeout to figure out how long we can wait without breaking into timeout.
174174
// Default to 1/4 of the remaining time in 1s steps.
175175
final long STEP_MS = 1000;
176-
long spentTime = System.currentTimeMillis() - startTime;
177-
long maxTries = (Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT) - spentTime) / STEP_MS / 4;
176+
long spentTimeNanos = System.nanoTime() - startTimeNanos;
177+
long maxTries = (Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT) - (spentTimeNanos / 1_000_000L)) / STEP_MS / 4;
178178

179179
long actualPauses = 0;
180180
long actualCycles = 0;

test/hotspot/jtreg/gc/stress/TestJNIBlockFullGC/TestJNIBlockFullGC.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2017 SAP SE and/or its affiliates. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -62,12 +62,12 @@ public Node(int load) {
6262
}
6363
}
6464

65-
public static void warmUp(long warmupEndTime, int size, long seed) {
65+
public static void warmUp(long warmupEndTimeNanos, int size, long seed) {
6666
Random r = new Random(seed);
6767
// First let the GC assume most of our objects will die.
6868
Node[] roots = new Node[size];
6969

70-
while (System.currentTimeMillis() < warmupEndTime) {
70+
while (System.nanoTime() - warmupEndTimeNanos < 0) {
7171
int index = (int) (r.nextDouble() * roots.length);
7272
roots[index] = new Node(1);
7373
}
@@ -78,7 +78,7 @@ public static void warmUp(long warmupEndTime, int size, long seed) {
7878
}
7979
}
8080

81-
public static void runTest(long endTime, int size, double alive, long seed) {
81+
public static void runTest(long endTimeNanos, int size, double alive, long seed) {
8282
Random r = new Random(seed);
8383
final int length = 10000;
8484
int[] array1 = new int[length];
@@ -91,7 +91,7 @@ public static void runTest(long endTime, int size, double alive, long seed) {
9191
int index = 0;
9292
roots[0] = new Node(0);
9393

94-
while (!hadError && (System.currentTimeMillis() < endTime)) {
94+
while (!hadError && (System.nanoTime() - endTimeNanos < 0)) {
9595
int test_val1 = TestCriticalArray0(array1);
9696

9797
if (r.nextDouble() > alive) {
@@ -136,15 +136,15 @@ public static void main(String[] args) throws Exception {
136136
int warmupThreads = Integer.parseInt(args[0]);
137137
System.out.println("# Warmup Threads = " + warmupThreads);
138138

139-
int warmupDuration = Integer.parseInt(args[1]);
140-
System.out.println("WarmUp Duration = " + warmupDuration);
139+
long warmupDurationNanos = 1_000_000L * Integer.parseInt(args[1]);
140+
System.out.println("WarmUp Duration Millis = " + args[1]);
141141
int warmupIterations = Integer.parseInt(args[2]);
142142
System.out.println("# Warmup Iterations = "+ warmupIterations);
143143

144144
int mainThreads = Integer.parseInt(args[3]);
145145
System.out.println("# Main Threads = " + mainThreads);
146-
int mainDuration = Integer.parseInt(args[4]);
147-
System.out.println("Main Duration = " + mainDuration);
146+
long mainDurationNanos = 1_000_000L * Integer.parseInt(args[4]);
147+
System.out.println("Main Duration Millis = " + args[4]);
148148
int mainIterations = Integer.parseInt(args[5]);
149149
System.out.println("# Main Iterations = " + mainIterations);
150150

@@ -154,12 +154,12 @@ public static void main(String[] args) throws Exception {
154154
Thread threads[] = new Thread[Math.max(warmupThreads, mainThreads)];
155155

156156
System.out.println("Start warm-up threads!");
157-
long warmupStartTime = System.currentTimeMillis();
157+
long warmupStartTimeNanos = System.nanoTime();
158158
for (int i = 0; i < warmupThreads; i++) {
159159
long seed = rng.nextLong();
160160
threads[i] = new Thread() {
161161
public void run() {
162-
warmUp(warmupStartTime + warmupDuration, warmupIterations, seed);
162+
warmUp(warmupStartTimeNanos + warmupDurationNanos, warmupIterations, seed);
163163
};
164164
};
165165
threads[i].start();
@@ -170,12 +170,12 @@ public void run() {
170170
System.gc();
171171
System.out.println("Keep alive a lot");
172172

173-
long startTime = System.currentTimeMillis();
173+
long startTimeNanos = System.nanoTime();
174174
for (int i = 0; i < mainThreads; i++) {
175175
long seed = rng.nextLong();
176176
threads[i] = new Thread() {
177177
public void run() {
178-
runTest(startTime + mainDuration, mainIterations, liveFrac, seed);
178+
runTest(startTimeNanos + mainDurationNanos, mainIterations, liveFrac, seed);
179179
};
180180
};
181181
threads[i].start();

test/hotspot/jtreg/gc/stress/TestMultiThreadStressRSet.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -111,7 +111,7 @@ public static void main(String args[]) {
111111
}
112112
long time = Long.parseLong(args[0]);
113113
int threads = Integer.parseInt(args[1]);
114-
new TestMultiThreadStressRSet().test(time * 1000, threads);
114+
new TestMultiThreadStressRSet().test(time * 1_000_000_000L, threads);
115115
}
116116

117117
/**
@@ -147,23 +147,23 @@ public TestMultiThreadStressRSet() {
147147
* <li> stops the Shifter thread
148148
* </ul>
149149
*
150-
* @param timeInMillis how long to stress
150+
* @param timeInNanos how long to stress
151151
* @param maxThreads the maximum number of Worker thread working together.
152152
*/
153-
public void test(long timeInMillis, int maxThreads) {
154-
if (timeInMillis <= 0 || maxThreads <= 0) {
153+
public void test(long timeInNanos, int maxThreads) {
154+
if (timeInNanos <= 0 || maxThreads <= 0) {
155155
throw new IllegalArgumentException("TEST BUG: be positive!");
156156
}
157-
System.out.println("%% Time to work: " + timeInMillis / 1000 + "s");
157+
System.out.println("%% Time to work: " + timeInNanos / 1_000_000_000L + "s");
158158
System.out.println("%% Number of threads: " + maxThreads);
159-
long finish = System.currentTimeMillis() + timeInMillis;
159+
long finishNanos = System.nanoTime() + timeInNanos;
160160
Shifter shift = new Shifter(this, 1000, (int) (windowSize * 0.9));
161161
shift.start();
162162
for (int i = 0; i < maxThreads; i++) {
163163
new Worker(this, 100).start();
164164
}
165165
try {
166-
while (System.currentTimeMillis() < finish && errorMessage == null) {
166+
while (System.nanoTime() - finishNanos < 0 && errorMessage == null) {
167167
Thread.sleep(100);
168168
}
169169
} catch (Throwable t) {

0 commit comments

Comments
 (0)