Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.
/ lanai Public archive

Commit db5da96

Browse files
author
Kim Barrett
committedDec 10, 2020
8257876: Avoid Reference.isEnqueued in tests
Reviewed-by: mchung, tschatzl
1 parent 4a839e9 commit db5da96

File tree

3 files changed

+65
-81
lines changed

3 files changed

+65
-81
lines changed
 

‎test/hotspot/jtreg/vmTestbase/gc/gctests/ReferencesGC/ReferencesGC.java

+53-61
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public class ReferencesGC extends ThreadedGCTest {
5353

5454
static int RANGE = 256;
5555
static float RATIO = (float) 1.0;
56+
static int REMOVE; // Initialized in parseArgs.
57+
static int RETAIN; // Initialized in parseArgs.
5658

5759
public static void main(String[] args) {
5860
parseArgs(args);
@@ -67,6 +69,8 @@ public static void parseArgs(String[] args) {
6769
RATIO = new Float(args[++i]).floatValue();
6870
}
6971
}
72+
REMOVE = (int) (RANGE * RATIO);
73+
RETAIN = RANGE - REMOVE;
7074
}
7175

7276
private class Worker implements Runnable {
@@ -76,13 +80,13 @@ private class Worker implements Runnable {
7680
static final int PHANTOM = 2;
7781
private ExecutionController stresser;
7882
int finalizationMaxTime = 1000 * 60 * runParams.getNumberOfThreads();
79-
int[] alive = new int[3];
80-
int[] enqued = new int[3];
83+
ReferenceQueue refq = null; // Reinitialized each time through loop
84+
int[] alive = null; // Reinitialized each time through loop
85+
int[] wrong = null; // Reinitialized each time through loop
8186
CircularLinkedList holder[] = new CircularLinkedList[RANGE];
8287
WeakReference wr[] = new WeakReference[RANGE];
8388
SoftReference sr[] = new SoftReference[RANGE];
8489
PhantomReference phr[] = new PhantomReference[RANGE];
85-
ReferenceQueue refq = new ReferenceQueue();
8690
GarbageProducer gp = GarbageUtils.getArrayProducers().get(0);
8791
int iter = 0;
8892

@@ -93,11 +97,11 @@ public void run() {
9397
}
9498

9599
while (stresser.continueExecution()) {
96-
int totalQ = 0;
100+
int totalLive = 0;
97101
try {
98102
refq = new ReferenceQueue();
99103
alive = new int[3];
100-
enqued = new int[3];
104+
wrong = new int[3];
101105
for (int j = 0; j < RANGE; j++) {
102106
holder[j] = new CircularLinkedList();
103107
holder[j].addNelements(300);
@@ -112,21 +116,21 @@ public void run() {
112116
}
113117

114118
for (int i = 0; i < RANGE; i++) {
115-
if (wr[i].isEnqueued()) {
116-
++totalQ;
119+
if (wr[i].refersTo(holder[i])) {
120+
++totalLive;
117121
}
118-
if (sr[i].isEnqueued()) {
119-
++totalQ;
122+
if (sr[i].refersTo(holder[i])) {
123+
++totalLive;
120124
}
121-
if (phr[i].isEnqueued()) {
122-
++totalQ;
125+
if (phr[i].refersTo(holder[i])) {
126+
++totalLive;
123127
}
124128
}
125-
if (totalQ != 0) {
126-
throw new TestFailure("There are " + totalQ + " references in the queue instead 0 before null-assigment.");
129+
if (totalLive != 3 * RANGE) {
130+
throw new TestFailure("There are " + (3 * RANGE - totalLive) + " references cleared before null-assigment.");
127131
}
128132

129-
for (int i = 0; i < (int) (RANGE * RATIO); i++) {
133+
for (int i = 0; i < REMOVE; i++) {
130134
holder[i] = null;
131135
}
132136

@@ -137,69 +141,57 @@ public void run() {
137141
// At this point OOME was thrown and accordingly to spec
138142
// all weak refs should be processed
139143

140-
alive = new int[3];
141-
enqued = new int[3];
142-
for (int i = 0; i < RANGE; i++) {
143-
if (wr[i].get() != null) {
144-
++alive[WEAK];
145-
}
146-
if (wr[i].isEnqueued()) {
147-
++enqued[WEAK];
148-
}
149-
if (sr[i].get() != null) {
150-
++alive[SOFT];
151-
}
152-
if (sr[i].isEnqueued()) {
153-
++enqued[SOFT];
154-
}
155-
if (phr[i].isEnqueued()) {
156-
++enqued[PHANTOM];
157-
}
158-
}
159-
160144
long waitTime = System.currentTimeMillis() + finalizationMaxTime;
161-
while (totalQ < (RANGE * RATIO * 3 * 0.9) && (System.currentTimeMillis() < waitTime)) {
162-
alive = new int[3];
163-
enqued = new int[3];
145+
int totalQ = 0;
146+
while ((totalQ < (3 * REMOVE)) && (System.currentTimeMillis() < waitTime)) {
147+
alive[WEAK] = alive[SOFT] = alive[PHANTOM] = 0;
148+
wrong[WEAK] = wrong[SOFT] = wrong[PHANTOM] = 0;
164149
for (int i = 0; i < RANGE; i++) {
165-
if (wr[i].get() != null) {
150+
if (!wr[i].refersTo(holder[i])) {
151+
++wrong[WEAK];
152+
} else if (holder[i] != null) {
166153
++alive[WEAK];
167154
}
168-
if (wr[i].isEnqueued()) {
169-
++enqued[WEAK];
170-
}
171-
if (sr[i].get() != null) {
155+
156+
if (!sr[i].refersTo(holder[i])) {
157+
++wrong[SOFT];
158+
} else if (holder[i] != null) {
172159
++alive[SOFT];
173160
}
174-
if (sr[i].isEnqueued()) {
175-
++enqued[SOFT];
161+
162+
if (!phr[i].refersTo(holder[i])) {
163+
++wrong[PHANTOM];
164+
} else if (holder[i] != null) {
165+
++alive[PHANTOM];
176166
}
177-
if (phr[i].isEnqueued()) {
178-
++enqued[PHANTOM];
167+
}
168+
169+
try {
170+
while (refq.remove(100) != null) {
171+
++totalQ;
179172
}
173+
} catch (InterruptedException ie) {
180174
}
181-
totalQ = (enqued[WEAK] + enqued[SOFT] + enqued[PHANTOM]);
182-
if (totalQ < (int) (3 * RANGE * RATIO * 0.9)) {
183-
log.debug("After null-assignment to " + (int) (RANGE * RATIO) +
184-
//" elements from " + lower + " to " + (upper - 1) +
185-
" and provoking gc found:\n\t" +
186-
enqued[WEAK] + " weak\n\t" +
187-
enqued[SOFT] + " soft\n\t" +
188-
enqued[PHANTOM] + " phantom " +
189-
" queuened refs and \n\t" +
190-
alive[WEAK] + " weak\n\t" +
191-
alive[SOFT] + " soft\n\t" +
192-
"alive refs.");
175+
if (totalQ < (3 * REMOVE)) {
176+
log.debug("After null-assignment to " + REMOVE +
177+
" referent values and provoking gc found:\n\t" +
178+
totalQ + " queued refs.");
193179
try {
194-
log.debug("sleeping to give gc one more chance ......");
180+
log.debug("sleeping to give reference processing more time ...");
195181
Thread.sleep(1000);
196182
} catch (InterruptedException ie) {
197183
}
198184
}
199185
}
200186
log.debug("iteration.... " + iter++);
201-
if (totalQ < (int) (3 * RANGE * RATIO * 0.9) || totalQ > (int) (3 * RANGE * RATIO)) {
202-
throw new TestFailure("Test failed");
187+
if (wrong[WEAK] != 0) {
188+
throw new TestFailure("Expected " + RETAIN + " weak references still alive: " + alive[WEAK]);
189+
} else if (wrong[SOFT] != 0) {
190+
throw new TestFailure("Expected " + RETAIN + " soft references still alive: " + alive[SOFT]);
191+
} else if (wrong[PHANTOM] != 0) {
192+
throw new TestFailure("Expected " + RETAIN + " phantom references still alive: " + alive[PHANTOM]);
193+
} else if (totalQ != (3 * REMOVE)) {
194+
throw new TestFailure("Expected " + (3 * REMOVE) + " references enqueued: " + totalQ);
203195
}
204196
}
205197
}

‎test/hotspot/jtreg/vmTestbase/gc/gctests/WeakReferenceGC/WeakReferenceGC.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,12 @@ private void persistentGC() {
201201
if (GarbageUtils.eatMemory(getExecutionController()) == 0) {
202202
return; // We were unable to provoke OOME before timeout is over
203203
}
204-
numEnqueued = 0; // We set counter to zero to avoid counting references twice
205-
for (int i = 0; i < numLists; i++) {
206-
if (wholder[i].isEnqueued()) {
204+
try {
205+
while ((numEnqueued < numLists) &&
206+
(refQueue.remove(1000) != null)) {
207207
numEnqueued++;
208208
}
209+
} catch (InterruptedException ie) {
209210
}
210211
}
211212
results.addElement((new Statistic(iter, numEnqueued)));

‎test/jdk/java/lang/ref/ReferenceEnqueue.java

+8-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2020, 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
@@ -51,20 +51,17 @@ static class WeakRef {
5151
}
5252

5353
void run() throws InterruptedException {
54+
boolean enqueued = false;
5455
System.gc();
5556
for (int i = 0; i < iterations; i++) {
5657
System.gc();
57-
if (ref.isEnqueued()) {
58-
break;
59-
}
60-
61-
Thread.sleep(100);
58+
enqueued = (queue.remove(100) == ref);
59+
if (enqueued) break;
6260
}
6361

64-
if (ref.isEnqueued() == false) {
62+
if (!enqueued) {
6563
// GC have not enqueued refWeak for the timeout period
66-
System.out.println("Reference not enqueued yet");
67-
return;
64+
throw new RuntimeException("Error: reference not enqueued");
6865
}
6966

7067
if (ref.enqueue() == true) {
@@ -73,12 +70,6 @@ void run() throws InterruptedException {
7370
throw new RuntimeException("Error: enqueue() returned true;"
7471
+ " expected false");
7572
}
76-
77-
if (queue.poll() == null) {
78-
// poll() should return ref enqueued by the GC
79-
throw new RuntimeException("Error: poll() returned null;"
80-
+ " expected ref object");
81-
}
8273
}
8374
}
8475

@@ -90,15 +81,15 @@ static class ExplicitEnqueue {
9081
ExplicitEnqueue() {
9182
this.refs.add(new SoftReference<>(new Object(), queue));
9283
this.refs.add(new WeakReference<>(new Object(), queue));
93-
// Can't test PhantomReference because get() always returns null.
84+
this.refs.add(new PhantomReference<>(new Object(), queue));
9485
}
9586

9687
void run() throws InterruptedException {
9788
for (Reference<Object> ref : refs) {
9889
if (ref.enqueue() == false) {
9990
throw new RuntimeException("Error: enqueue failed");
10091
}
101-
if (ref.get() != null) {
92+
if (!ref.refersTo(null)) {
10293
throw new RuntimeException("Error: referent must be cleared");
10394
}
10495
}

0 commit comments

Comments
 (0)
This repository has been archived.