Skip to content

Commit 543851d

Browse files
committed
8289607: Change hotspot/jtreg tests to not use Thread.suspend/resume
Reviewed-by: sspitsyn, cjplummer
1 parent e2f8251 commit 543851d

File tree

16 files changed

+225
-230
lines changed

16 files changed

+225
-230
lines changed

src/hotspot/share/prims/jvmtiEnvBase.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,7 @@ JvmtiEnvBase::resume_thread(oop thread_oop, JavaThread* java_thread, bool single
16661666
assert(single_resume || is_virtual, "ResumeAllVirtualThreads should never resume non-virtual threads");
16671667
if (java_thread->is_suspended()) {
16681668
if (!JvmtiSuspendControl::resume(java_thread)) {
1669-
return JVMTI_ERROR_INTERNAL;
1669+
return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
16701670
}
16711671
}
16721672
}

test/hotspot/jtreg/TEST.quick-groups

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2018, 2022, 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
@@ -103,8 +103,8 @@ vmTestbase_nsk_monitoring_quick = \
103103
vmTestbase/nsk/monitoring/ThreadInfo/getLockName/getlockname001/TestDescription.java \
104104
vmTestbase/nsk/monitoring/ThreadInfo/getLockOwnerName/getlockownername001/TestDescription.java \
105105
vmTestbase/nsk/monitoring/ThreadInfo/isInNative/isinnative001/TestDescription.java \
106-
vmTestbase/nsk/monitoring/ThreadInfo/isSuspended/issuspended001/TestDescription.java \
107-
vmTestbase/nsk/monitoring/ThreadInfo/isSuspended/issuspended002/TestDescription.java \
106+
vmTestbase/nsk/monitoring/ThreadInfo/isSuspended/issuspended001.java \
107+
vmTestbase/nsk/monitoring/ThreadInfo/isSuspended/issuspended002.java \
108108
vmTestbase/nsk/monitoring/ThreadMXBean/findMonitorDeadlockedThreads/find001/TestDescription.java \
109109
vmTestbase/nsk/monitoring/ThreadMXBean/findMonitorDeadlockedThreads/find002/TestDescription.java \
110110
vmTestbase/nsk/monitoring/ThreadMXBean/findMonitorDeadlockedThreads/find003/TestDescription.java \

test/hotspot/jtreg/runtime/handshake/HandshakeDirectTest.java

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2022, 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
@@ -33,6 +33,8 @@
3333
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -XX:GuaranteedSafepointInterval=10 -XX:+HandshakeALot -XX:+SafepointALot HandshakeDirectTest
3434
*/
3535

36+
import jvmti.JVMTIUtils;
37+
3638
import java.util.concurrent.atomic.AtomicInteger;
3739
import java.util.concurrent.ThreadLocalRandom;
3840
import java.util.concurrent.Semaphore;
@@ -46,6 +48,26 @@ public class HandshakeDirectTest implements Runnable {
4648
static Object[] locks = new Object[WORKING_THREADS];
4749
static AtomicInteger handshakeCount = new AtomicInteger(0);
4850

51+
static void suspendThread(Thread t) {
52+
try {
53+
JVMTIUtils.suspendThread(t);
54+
} catch (JVMTIUtils.JvmtiException e) {
55+
if (e.getCode() != JVMTIUtils.JVMTI_ERROR_THREAD_NOT_ALIVE) {
56+
throw e;
57+
}
58+
}
59+
}
60+
61+
static void resumeThread(Thread t) {
62+
try {
63+
JVMTIUtils.resumeThread(t);
64+
} catch (JVMTIUtils.JvmtiException e) {
65+
if (e.getCode() != JVMTIUtils.JVMTI_ERROR_THREAD_NOT_ALIVE) {
66+
throw e;
67+
}
68+
}
69+
}
70+
4971
@Override
5072
public void run() {
5173
int me = Integer.parseInt(Thread.currentThread().getName());
@@ -91,12 +113,12 @@ public static void main(String... args) throws Exception {
91113
public void run() {
92114
while (true) {
93115
int i = ThreadLocalRandom.current().nextInt(0, WORKING_THREADS - 1);
94-
workingThreads[i].suspend();
116+
suspendThread(workingThreads[i]);
95117
try {
96118
Thread.sleep(1); // sleep for 1 ms
97119
} catch(InterruptedException ie) {
98120
}
99-
workingThreads[i].resume();
121+
resumeThread(workingThreads[i]);
100122
}
101123
}
102124
};

test/hotspot/jtreg/runtime/handshake/HandshakeSuspendExitTest.java

+31-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2022, 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
@@ -30,27 +30,52 @@
3030
* @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:GuaranteedSafepointInterval=1 -XX:+HandshakeALot HandshakeSuspendExitTest
3131
*/
3232

33+
import jvmti.JVMTIUtils;
34+
3335
public class HandshakeSuspendExitTest implements Runnable {
3436

3537
static Thread[] _suspend_threads = new Thread[16];
3638
static volatile boolean _exit_now = false;
3739
static java.util.concurrent.Semaphore _sem = new java.util.concurrent.Semaphore(0);
3840

41+
static void suspendThread(Thread t) {
42+
try {
43+
JVMTIUtils.suspendThread(t);
44+
} catch (JVMTIUtils.JvmtiException e) {
45+
if (e.getCode() != JVMTIUtils.JVMTI_ERROR_THREAD_SUSPENDED
46+
&& e.getCode() != JVMTIUtils.JVMTI_ERROR_THREAD_NOT_ALIVE) {
47+
throw e;
48+
}
49+
}
50+
}
51+
52+
static void resumeThread(Thread t) {
53+
try {
54+
JVMTIUtils.resumeThread(t);
55+
} catch (JVMTIUtils.JvmtiException e) {
56+
if (e.getCode() != JVMTIUtils.JVMTI_ERROR_THREAD_NOT_SUSPENDED
57+
&& e.getCode() != JVMTIUtils.JVMTI_ERROR_THREAD_NOT_ALIVE) {
58+
throw e;
59+
}
60+
}
61+
}
62+
3963
@Override
4064
public void run() {
4165
_sem.release();
4266
while (!_exit_now) {
4367
// Leave last 2 threads running.
4468
for (int i = 0; i < _suspend_threads.length - 2; i++) {
4569
if (Thread.currentThread() != _suspend_threads[i]) {
46-
_suspend_threads[i].suspend();
47-
_suspend_threads[i].resume();
70+
suspendThread(_suspend_threads[i]);
71+
resumeThread(_suspend_threads[i]);
4872
}
4973
}
5074
}
5175
_sem.release();
5276
}
5377

78+
5479
public static void main(String... args) throws Exception {
5580
HandshakeSuspendExitTest test = new HandshakeSuspendExitTest();
5681
// Fire-up suspend threads.
@@ -74,10 +99,10 @@ public static void main(String... args) throws Exception {
7499

75100
// Try to suspend them.
76101
for (Thread thr : exit_threads) {
77-
thr.suspend();
102+
suspendThread(thr);
78103
}
79104
for (Thread thr : exit_threads) {
80-
thr.resume();
105+
resumeThread(thr);
81106
}
82107

83108
// Start exit and join.
@@ -88,7 +113,7 @@ public static void main(String... args) throws Exception {
88113
// each other at exactly the same time so they can see
89114
// _exit_now and check in via the semaphore.
90115
for (Thread thr : _suspend_threads) {
91-
thr.resume();
116+
resumeThread(thr);
92117
}
93118
while (_sem.tryAcquire()) {
94119
--waiting;

test/hotspot/jtreg/runtime/handshake/SuspendBlocked.java

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2022, 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
@@ -25,12 +25,14 @@
2525
/*
2626
* @test SuspendBlocked
2727
* @bug 8270085
28-
* @library /test/lib
28+
* @library /test/lib /testlibrary
2929
* @build SuspendBlocked
3030
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
3131
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI SuspendBlocked
3232
*/
3333

34+
import jvmti.JVMTIUtils;
35+
3436
import jdk.test.lib.Asserts;
3537
import jdk.test.whitebox.WhiteBox;
3638

@@ -41,10 +43,16 @@ public static void main(String... args) throws Exception {
4143
suspend_thread.start();
4244
WhiteBox wb = WhiteBox.getWhiteBox();
4345
for (int i = 0; i < 100; i++) {
44-
suspend_thread.suspend();
45-
wb.lockAndBlock(/* suspender= */ true);
46-
suspend_thread.resume();
47-
Thread.sleep(1);
46+
try {
47+
JVMTIUtils.suspendThread(suspend_thread);
48+
wb.lockAndBlock(/* suspender= */ true);
49+
JVMTIUtils.resumeThread(suspend_thread);
50+
Thread.sleep(1);
51+
} catch (JVMTIUtils.JvmtiException e) {
52+
if (e.getCode() != JVMTIUtils.JVMTI_ERROR_THREAD_NOT_ALIVE) {
53+
throw e;
54+
}
55+
}
4856
}
4957
suspend_thread.join();
5058
}

test/hotspot/jtreg/runtime/jni/terminatedThread/TestTerminatedThread.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@
2929
* @comment Calling pthread_getcpuclockid() with invalid pid leads to undefined
3030
* behavior in musl libc (see 8240187).
3131
* @requires !vm.musl
32+
* @library /testlibrary
3233
* @summary Basic test of Thread and ThreadMXBean queries on a natively
3334
* attached thread that has failed to detach before terminating.
3435
* @comment The native code only supports POSIX so no windows testing
3536
* @run main/othervm/native TestTerminatedThread
3637
*/
3738

39+
import jvmti.JVMTIUtils;
40+
3841
public class TestTerminatedThread {
3942

4043
static native Thread createTerminatedThread();
@@ -63,9 +66,9 @@ public static void main(String[] args) throws Throwable {
6366
", in state: " + t.getState());
6467

6568
System.out.println("Calling suspend ...");
66-
t.suspend();
69+
JVMTIUtils.suspendThread(t);
6770
System.out.println("Calling resume ...");
68-
t.resume();
71+
JVMTIUtils.resumeThread(t);
6972
System.out.println("Calling getStackTrace ...");
7073
StackTraceElement[] stack = t.getStackTrace();
7174
System.out.println(java.util.Arrays.toString(stack));

test/hotspot/jtreg/serviceability/jvmti/thread/GetThreadState/thrstat02/thrstat02.java

+2-19
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
* VM Testbase readme:
3131
* DESCRIPTION
3232
* The test exercises JVMDI function GetThreadState. Java program launches
33-
* a thread and for various thread states calls Thread.suspend()/resume()
34-
* methods or JVMDI functions SuspendThread/ResumeThread. Then native method
33+
* a thread and for various thread states calls
34+
* JVMTI functions SuspendThread/ResumeThread. Then native method
3535
* checkStatus is invoked. This method calls GetThreadState and checks if
3636
* the returned values are correct and JVMTI_THREAD_STATE_SUSPENDED bit
3737
* is set (or clear after resume).
@@ -120,30 +120,13 @@ void meth() throws InterruptedException {
120120
checkStatus(STATUS_MONITOR, false);
121121
System.out.println("thrstat02.meth after checkStatus(STATUS_MONITOR,false)");
122122

123-
thr.suspend();
124-
System.out.println("thrstat02.meth after thr.suspend()");
125-
checkStatus(STATUS_MONITOR, true);
126-
System.out.println("thrstat02.meth after checkStatus(STATUS_MONITOR,true)");
127-
128-
thr.resume();
129-
System.out.println("thrstat02.meth after thr.resume()");
130-
checkStatus(STATUS_MONITOR, false);
131-
System.out.println("thrstat02.meth after checkStatus(STATUS_MONITOR,false)");
132123
}
133124

134125
runningBarrier.await();
135126
checkStatus(STATUS_RUNNING, false);
136-
thr.suspend();
137-
checkStatus(STATUS_RUNNING, true);
138-
thr.resume();
139-
checkStatus(STATUS_RUNNING, false);
140127
thr.letItGo();
141128

142129
synchronized (endingMonitor) {
143-
checkStatus(STATUS_WAIT, false);
144-
thr.suspend();
145-
checkStatus(STATUS_WAIT, true);
146-
thr.resume();
147130
checkStatus(STATUS_WAIT, false);
148131
endingMonitor.val++;
149132
endingMonitor.notifyAll();

test/hotspot/jtreg/testlibrary/jvmti/JVMTIUtils.java

+42
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,31 @@
2525

2626
public class JVMTIUtils {
2727

28+
public static int JVMTI_ERROR_NONE = 0;
29+
30+
public static int JVMTI_ERROR_THREAD_NOT_SUSPENDED = 13;
31+
public static int JVMTI_ERROR_THREAD_SUSPENDED = 14;
32+
public static int JVMTI_ERROR_THREAD_NOT_ALIVE = 15;
33+
34+
35+
public static class JvmtiException extends RuntimeException {
36+
37+
private int code;
38+
39+
public JvmtiException(int code) {
40+
this.code = code;
41+
}
42+
43+
public int getCode() {
44+
return code;
45+
}
46+
47+
@Override
48+
public String getMessage(){
49+
return "JVMTI ERROR: " + code;
50+
}
51+
}
52+
2853
private static native int init();
2954

3055
static {
@@ -39,4 +64,21 @@ public class JVMTIUtils {
3964
public static void stopThread(Thread t) {
4065
stopThread(t, new ThreadDeath());
4166
}
67+
68+
private static native int suspendThread0(Thread t);
69+
private static native int resumeThread0(Thread t);
70+
71+
public static void suspendThread(Thread t) {
72+
int err = suspendThread0(t);
73+
if (err != JVMTI_ERROR_NONE) {
74+
throw new JvmtiException(err);
75+
}
76+
}
77+
public static void resumeThread(Thread t) {
78+
int err = resumeThread0(t);
79+
if (err != JVMTI_ERROR_NONE) {
80+
throw new JvmtiException(err);
81+
}
82+
}
83+
4284
}

test/hotspot/jtreg/testlibrary/jvmti/libJvmtiUtils.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Java_jvmti_JVMTIUtils_init(JNIEnv *jni, jclass cls) {
3939
}
4040
jvmtiCapabilities caps;
4141
memset(&caps, 0, sizeof (caps));
42+
caps.can_suspend = 1;
4243
caps.can_signal_thread = 1;
4344
jvmtiError err = jvmti->AddCapabilities(&caps);
4445
if (err != JVMTI_ERROR_NONE) {
@@ -52,10 +53,20 @@ JNIEXPORT void JNICALL
5253
Java_jvmti_JVMTIUtils_stopThread(JNIEnv *jni, jclass cls, jthread thread, jobject exception) {
5354
jvmtiError err = jvmti->StopThread(thread, exception);
5455
if (err == JVMTI_ERROR_THREAD_NOT_ALIVE) {
55-
LOG("JVMTI_ERROR_THREAD_NOT_ALIVE happened");
56+
LOG("JVMTI_ERROR_THREAD_NOT_ALIVE happened\n");
5657
return;
5758
}
5859
check_jvmti_status(jni, err, "Error during StopThread()");
5960
}
6061

62+
JNIEXPORT jint JNICALL
63+
Java_jvmti_JVMTIUtils_suspendThread0(JNIEnv *jni, jclass cls, jthread thread) {
64+
return jvmti->SuspendThread(thread);
65+
}
66+
67+
JNIEXPORT jint JNICALL
68+
Java_jvmti_JVMTIUtils_resumeThread0(JNIEnv *jni, jclass cls, jthread thread) {
69+
return jvmti->ResumeThread(thread);
70+
}
71+
6172
}

0 commit comments

Comments
 (0)