8262881: port JVM/DI tests from JDK-4413752 to JVM/TI
Reviewed-by: sspitsyn, rehn
This commit is contained in:
parent
06e6b1f7ae
commit
1ca4abe9f2
@ -0,0 +1,399 @@
|
||||
/*
|
||||
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4413752 8262881
|
||||
* @summary Test SuspendThread with ObjectMonitor enter.
|
||||
* @requires vm.jvmti
|
||||
* @library /test/lib
|
||||
* @compile SuspendWithObjectMonitorEnter.java
|
||||
* @run main/othervm/native -agentlib:SuspendWithObjectMonitorEnter SuspendWithObjectMonitorEnter
|
||||
*/
|
||||
|
||||
import java.io.PrintStream;
|
||||
|
||||
//
|
||||
// main blocker contender resumer
|
||||
// ================= ================ =================== ================
|
||||
// launch blocker
|
||||
// <launch returns> blocker running
|
||||
// launch contender enter threadLock
|
||||
// <launch returns> wait for notify contender running
|
||||
// launch resumer : block on threadLock
|
||||
// <launch returns> : : resumer running
|
||||
// suspend contender : <suspended> wait for notify
|
||||
// <ready to test> : : :
|
||||
// : : : :
|
||||
// notify blocker wait finishes : :
|
||||
// notify resumer exit threadLock : wait finishes
|
||||
// join blocker : : enter threadLock
|
||||
// <join returns> blocker exits <resumed> resume contender
|
||||
// join resumer : exit threadLock
|
||||
// <join returns> enter threadLock resumer exits
|
||||
// join contender exit threadLock
|
||||
// <join returns> contender exits
|
||||
//
|
||||
|
||||
public class SuspendWithObjectMonitorEnter {
|
||||
private static final String AGENT_LIB = "SuspendWithObjectMonitorEnter";
|
||||
private static final int exit_delta = 95;
|
||||
|
||||
private static final int DEF_TIME_MAX = 60; // default max # secs to test
|
||||
private static final int JOIN_MAX = 30; // max # secs to wait for join
|
||||
|
||||
public static final int TS_INIT = 1; // initial testState
|
||||
public static final int TS_BLOCKER_RUNNING = 2; // blocker is running
|
||||
public static final int TS_CONTENDER_RUNNING = 3; // contender is running
|
||||
public static final int TS_RESUMER_RUNNING = 4; // resumer is running
|
||||
public static final int TS_CALL_SUSPEND = 5; // call suspend on contender
|
||||
public static final int TS_DONE_BLOCKING = 6; // done blocking threadLock
|
||||
public static final int TS_READY_TO_RESUME = 7; // ready to resume contender
|
||||
public static final int TS_CALL_RESUME = 8; // call resume on contender
|
||||
public static final int TS_CONTENDER_DONE = 9; // contender has run; done
|
||||
|
||||
public static Object barrierLaunch = new Object(); // controls thread launch
|
||||
public static Object barrierBlocker = new Object(); // controls blocker
|
||||
public static Object barrierResumer = new Object(); // controls resumer
|
||||
public static Object threadLock = new Object(); // testing object
|
||||
|
||||
public static long count = 0;
|
||||
public static boolean printDebug = false;
|
||||
public volatile static int testState;
|
||||
|
||||
private static void log(String msg) { System.out.println(msg); }
|
||||
|
||||
native static int suspendThread(SuspendWithObjectMonitorEnterWorker thr);
|
||||
native static int wait4ContendedEnter(SuspendWithObjectMonitorEnterWorker thr);
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
try {
|
||||
System.loadLibrary(AGENT_LIB);
|
||||
log("Loaded library: " + AGENT_LIB);
|
||||
} catch (UnsatisfiedLinkError ule) {
|
||||
log("Failed to load library: " + AGENT_LIB);
|
||||
log("java.library.path: " + System.getProperty("java.library.path"));
|
||||
throw ule;
|
||||
}
|
||||
|
||||
int timeMax = 0;
|
||||
if (args.length == 0) {
|
||||
timeMax = DEF_TIME_MAX;
|
||||
} else {
|
||||
int argIndex = 0;
|
||||
int argsLeft = args.length;
|
||||
if (args[0].equals("-p")) {
|
||||
printDebug = true;
|
||||
argIndex = 1;
|
||||
argsLeft--;
|
||||
}
|
||||
if (argsLeft == 0) {
|
||||
timeMax = DEF_TIME_MAX;
|
||||
} else if (argsLeft == 1) {
|
||||
try {
|
||||
timeMax = Integer.parseUnsignedInt(args[argIndex]);
|
||||
} catch (NumberFormatException nfe) {
|
||||
System.err.println("'" + args[argIndex] +
|
||||
"': invalid timeMax value.");
|
||||
usage();
|
||||
}
|
||||
} else {
|
||||
usage();
|
||||
}
|
||||
}
|
||||
|
||||
System.exit(run(timeMax, System.out) + exit_delta);
|
||||
}
|
||||
|
||||
public static void logDebug(String mesg) {
|
||||
if (printDebug) {
|
||||
System.err.println(Thread.currentThread().getName() + ": " + mesg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void usage() {
|
||||
System.err.println("Usage: " + AGENT_LIB + " [-p][time_max]");
|
||||
System.err.println("where:");
|
||||
System.err.println(" -p ::= print debug info");
|
||||
System.err.println(" time_max ::= max looping time in seconds");
|
||||
System.err.println(" (default is " + DEF_TIME_MAX +
|
||||
" seconds)");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
public static int run(int timeMax, PrintStream out) {
|
||||
return (new SuspendWithObjectMonitorEnter()).doWork(timeMax, out);
|
||||
}
|
||||
|
||||
public static void checkTestState(int exp) {
|
||||
if (testState != exp) {
|
||||
System.err.println("Failure at " + count + " loops.");
|
||||
throw new InternalError("Unexpected test state value: "
|
||||
+ "expected=" + exp + " actual=" + testState);
|
||||
}
|
||||
}
|
||||
|
||||
public int doWork(int timeMax, PrintStream out) {
|
||||
SuspendWithObjectMonitorEnterWorker blocker; // blocker thread
|
||||
SuspendWithObjectMonitorEnterWorker contender; // contender thread
|
||||
SuspendWithObjectMonitorEnterWorker resumer; // resumer thread
|
||||
|
||||
System.out.println("About to execute for " + timeMax + " seconds.");
|
||||
|
||||
long start_time = System.currentTimeMillis();
|
||||
while (System.currentTimeMillis() < start_time + (timeMax * 1000)) {
|
||||
count++;
|
||||
testState = TS_INIT; // starting the test loop
|
||||
|
||||
// launch the blocker thread
|
||||
synchronized (barrierLaunch) {
|
||||
blocker = new SuspendWithObjectMonitorEnterWorker("blocker");
|
||||
blocker.start();
|
||||
|
||||
while (testState != TS_BLOCKER_RUNNING) {
|
||||
try {
|
||||
barrierLaunch.wait(0); // wait until it is running
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// launch the contender thread
|
||||
synchronized (barrierLaunch) {
|
||||
contender = new SuspendWithObjectMonitorEnterWorker("contender");
|
||||
contender.start();
|
||||
|
||||
while (testState != TS_CONTENDER_RUNNING) {
|
||||
try {
|
||||
barrierLaunch.wait(0); // wait until it is running
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// launch the resumer thread
|
||||
synchronized (barrierLaunch) {
|
||||
resumer = new SuspendWithObjectMonitorEnterWorker("resumer", contender);
|
||||
resumer.start();
|
||||
|
||||
while (testState != TS_RESUMER_RUNNING) {
|
||||
try {
|
||||
barrierLaunch.wait(0); // wait until it is running
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// wait for the contender thread to block
|
||||
logDebug("before contended enter wait");
|
||||
int retCode = wait4ContendedEnter(contender);
|
||||
if (retCode != 0) {
|
||||
throw new RuntimeException("error in JVMTI GetThreadState: " +
|
||||
"retCode=" + retCode);
|
||||
}
|
||||
logDebug("done contended enter wait");
|
||||
|
||||
checkTestState(TS_RESUMER_RUNNING);
|
||||
testState = TS_CALL_SUSPEND;
|
||||
logDebug("before suspend thread");
|
||||
retCode = suspendThread(contender);
|
||||
if (retCode != 0) {
|
||||
throw new RuntimeException("error in JVMTI SuspendThread: " +
|
||||
"retCode=" + retCode);
|
||||
}
|
||||
logDebug("suspended thread");
|
||||
|
||||
//
|
||||
// At this point, all of the child threads are running
|
||||
// and we can get to meat of the test:
|
||||
//
|
||||
// - suspended threadLock contender
|
||||
// - a threadLock exit in the blocker thread
|
||||
// - a threadLock enter in the resumer thread
|
||||
// - resumption of the contender thread
|
||||
// - a threadLock enter in the freshly resumed contender thread
|
||||
//
|
||||
synchronized (barrierBlocker) {
|
||||
checkTestState(TS_CALL_SUSPEND);
|
||||
|
||||
// tell blocker thread to exit threadLock
|
||||
testState = TS_DONE_BLOCKING;
|
||||
barrierBlocker.notify();
|
||||
}
|
||||
|
||||
synchronized (barrierResumer) {
|
||||
// tell resumer thread to resume contender thread
|
||||
testState = TS_READY_TO_RESUME;
|
||||
barrierResumer.notify();
|
||||
|
||||
// Can't call checkTestState() here because the
|
||||
// resumer thread may have already resumed the
|
||||
// contender thread.
|
||||
}
|
||||
|
||||
try {
|
||||
blocker.join();
|
||||
resumer.join(JOIN_MAX * 1000);
|
||||
if (resumer.isAlive()) {
|
||||
System.err.println("Failure at " + count + " loops.");
|
||||
throw new InternalError("resumer thread is stuck");
|
||||
}
|
||||
contender.join(JOIN_MAX * 1000);
|
||||
if (contender.isAlive()) {
|
||||
System.err.println("Failure at " + count + " loops.");
|
||||
throw new InternalError("contender thread is stuck");
|
||||
}
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
|
||||
checkTestState(TS_CONTENDER_DONE);
|
||||
}
|
||||
|
||||
System.out.println("Executed " + count + " loops in " + timeMax +
|
||||
" seconds.");
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
class SuspendWithObjectMonitorEnterWorker extends Thread {
|
||||
private SuspendWithObjectMonitorEnterWorker target; // target for resume operation
|
||||
|
||||
public SuspendWithObjectMonitorEnterWorker(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public SuspendWithObjectMonitorEnterWorker(String name, SuspendWithObjectMonitorEnterWorker target) {
|
||||
super(name);
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
native static int resumeThread(SuspendWithObjectMonitorEnterWorker thr);
|
||||
|
||||
public void run() {
|
||||
SuspendWithObjectMonitorEnter.logDebug("thread running");
|
||||
|
||||
//
|
||||
// Launch the blocker thread:
|
||||
// - grabs threadLock
|
||||
// - holds threadLock until we tell it let go
|
||||
// - releases threadLock
|
||||
//
|
||||
if (getName().equals("blocker")) {
|
||||
// grab threadLock before we tell main we are running
|
||||
SuspendWithObjectMonitorEnter.logDebug("before enter threadLock");
|
||||
synchronized(SuspendWithObjectMonitorEnter.threadLock) {
|
||||
SuspendWithObjectMonitorEnter.logDebug("enter threadLock");
|
||||
|
||||
SuspendWithObjectMonitorEnter.checkTestState(SuspendWithObjectMonitorEnter.TS_INIT);
|
||||
|
||||
synchronized(SuspendWithObjectMonitorEnter.barrierBlocker) {
|
||||
synchronized(SuspendWithObjectMonitorEnter.barrierLaunch) {
|
||||
// tell main we are running
|
||||
SuspendWithObjectMonitorEnter.testState = SuspendWithObjectMonitorEnter.TS_BLOCKER_RUNNING;
|
||||
SuspendWithObjectMonitorEnter.barrierLaunch.notify();
|
||||
}
|
||||
SuspendWithObjectMonitorEnter.logDebug("thread waiting");
|
||||
// TS_READY_TO_RESUME is set right after TS_DONE_BLOCKING
|
||||
// is set so either can get the blocker thread out of
|
||||
// this wait() wrapper:
|
||||
while (SuspendWithObjectMonitorEnter.testState != SuspendWithObjectMonitorEnter.TS_DONE_BLOCKING &&
|
||||
SuspendWithObjectMonitorEnter.testState != SuspendWithObjectMonitorEnter.TS_READY_TO_RESUME) {
|
||||
try {
|
||||
// wait for main to tell us when to exit threadLock
|
||||
SuspendWithObjectMonitorEnter.barrierBlocker.wait(0);
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
SuspendWithObjectMonitorEnter.logDebug("exit threadLock");
|
||||
}
|
||||
}
|
||||
//
|
||||
// Launch the contender thread:
|
||||
// - tries to grab the threadLock
|
||||
// - grabs threadLock
|
||||
// - releases threadLock
|
||||
//
|
||||
else if (getName().equals("contender")) {
|
||||
synchronized(SuspendWithObjectMonitorEnter.barrierLaunch) {
|
||||
// tell main we are running
|
||||
SuspendWithObjectMonitorEnter.testState = SuspendWithObjectMonitorEnter.TS_CONTENDER_RUNNING;
|
||||
SuspendWithObjectMonitorEnter.barrierLaunch.notify();
|
||||
}
|
||||
|
||||
SuspendWithObjectMonitorEnter.logDebug("before enter threadLock");
|
||||
synchronized(SuspendWithObjectMonitorEnter.threadLock) {
|
||||
SuspendWithObjectMonitorEnter.logDebug("enter threadLock");
|
||||
|
||||
SuspendWithObjectMonitorEnter.checkTestState(SuspendWithObjectMonitorEnter.TS_CALL_RESUME);
|
||||
SuspendWithObjectMonitorEnter.testState = SuspendWithObjectMonitorEnter.TS_CONTENDER_DONE;
|
||||
|
||||
SuspendWithObjectMonitorEnter.logDebug("exit threadLock");
|
||||
}
|
||||
}
|
||||
//
|
||||
// Launch the resumer thread:
|
||||
// - tries to grab the threadLock (should not block!)
|
||||
// - grabs threadLock
|
||||
// - resumes the contended thread
|
||||
// - releases threadLock
|
||||
//
|
||||
else if (getName().equals("resumer")) {
|
||||
synchronized(SuspendWithObjectMonitorEnter.barrierResumer) {
|
||||
synchronized(SuspendWithObjectMonitorEnter.barrierLaunch) {
|
||||
// tell main we are running
|
||||
SuspendWithObjectMonitorEnter.testState = SuspendWithObjectMonitorEnter.TS_RESUMER_RUNNING;
|
||||
SuspendWithObjectMonitorEnter.barrierLaunch.notify();
|
||||
}
|
||||
SuspendWithObjectMonitorEnter.logDebug("thread waiting");
|
||||
while (SuspendWithObjectMonitorEnter.testState != SuspendWithObjectMonitorEnter.TS_READY_TO_RESUME) {
|
||||
try {
|
||||
// wait for main to tell us when to continue
|
||||
SuspendWithObjectMonitorEnter.barrierResumer.wait(0);
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SuspendWithObjectMonitorEnter.logDebug("before enter threadLock");
|
||||
synchronized(SuspendWithObjectMonitorEnter.threadLock) {
|
||||
SuspendWithObjectMonitorEnter.logDebug("enter threadLock");
|
||||
|
||||
SuspendWithObjectMonitorEnter.checkTestState(SuspendWithObjectMonitorEnter.TS_READY_TO_RESUME);
|
||||
SuspendWithObjectMonitorEnter.testState = SuspendWithObjectMonitorEnter.TS_CALL_RESUME;
|
||||
|
||||
// resume the contender thread so contender.join() can work
|
||||
SuspendWithObjectMonitorEnter.logDebug("before resume thread");
|
||||
int retCode = resumeThread(target);
|
||||
if (retCode != 0) {
|
||||
throw new RuntimeException("error in JVMTI ResumeThread: " +
|
||||
"retCode=" + retCode);
|
||||
}
|
||||
SuspendWithObjectMonitorEnter.logDebug("resumed thread");
|
||||
|
||||
SuspendWithObjectMonitorEnter.logDebug("exit threadLock");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "jvmti.h"
|
||||
|
||||
extern "C" {
|
||||
|
||||
static jvmtiEnv* jvmti = NULL;
|
||||
|
||||
#define LOG(...) \
|
||||
do { \
|
||||
printf(__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
fflush(stdout); \
|
||||
} while (0)
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_SuspendWithObjectMonitorEnter_suspendThread(JNIEnv *jni, jclass cls, jthread thr) {
|
||||
return jvmti->SuspendThread(thr);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_SuspendWithObjectMonitorEnter_wait4ContendedEnter(JNIEnv *jni, jclass cls, jthread thr) {
|
||||
jvmtiError err;
|
||||
jint thread_state;
|
||||
do {
|
||||
err = jvmti->GetThreadState(thr, &thread_state);
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
} while ((thread_state & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) == 0);
|
||||
return err;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_SuspendWithObjectMonitorEnterWorker_resumeThread(JNIEnv *jni, jclass cls, jthread thr) {
|
||||
return jvmti->ResumeThread(thr);
|
||||
}
|
||||
|
||||
|
||||
/** Agent library initialization. */
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
|
||||
LOG("\nAgent_OnLoad started");
|
||||
|
||||
// create JVMTI environment
|
||||
if (jvm->GetEnv((void **) (&jvmti), JVMTI_VERSION) != JNI_OK) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
// add specific capabilities for suspending thread
|
||||
jvmtiCapabilities suspendCaps;
|
||||
memset(&suspendCaps, 0, sizeof(suspendCaps));
|
||||
suspendCaps.can_suspend = 1;
|
||||
|
||||
jvmtiError err = jvmti->AddCapabilities(&suspendCaps);
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
LOG("Agent_OnLoad finished\n");
|
||||
return JNI_OK;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,371 @@
|
||||
/*
|
||||
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4413752 8262881
|
||||
* @summary Test SuspendThread with ObjectMonitor wait.
|
||||
* @requires vm.jvmti
|
||||
* @library /test/lib
|
||||
* @compile SuspendWithObjectMonitorWait.java
|
||||
* @run main/othervm/native -agentlib:SuspendWithObjectMonitorWait SuspendWithObjectMonitorWait
|
||||
*/
|
||||
|
||||
import java.io.PrintStream;
|
||||
|
||||
//
|
||||
// main waiter resumer
|
||||
// ================= ================== ===================
|
||||
// launch waiter
|
||||
// <launch returns> waiter running
|
||||
// launch resumer enter threadLock
|
||||
// <launch returns> threadLock.wait() resumer running
|
||||
// enter threadLock : wait for notify
|
||||
// threadLock.notify wait finishes :
|
||||
// : reenter blocks :
|
||||
// suspend waiter <suspended> :
|
||||
// exit threadLock : :
|
||||
// <ready to test> : :
|
||||
// : : :
|
||||
// notify resumer : wait finishes
|
||||
// join resumer : enter threadLock
|
||||
// : <resumed> resume waiter
|
||||
// : : exit threadLock
|
||||
// : reenter threadLock :
|
||||
// <join returns> : resumer exits
|
||||
// join waiter :
|
||||
// <join returns> waiter exits
|
||||
//
|
||||
|
||||
public class SuspendWithObjectMonitorWait {
|
||||
private static final String AGENT_LIB = "SuspendWithObjectMonitorWait";
|
||||
private static final int exit_delta = 95;
|
||||
|
||||
private static final int DEF_TIME_MAX = 60; // default max # secs to test
|
||||
private static final int JOIN_MAX = 30; // max # secs to wait for join
|
||||
|
||||
public static final int TS_INIT = 1; // initial testState
|
||||
public static final int TS_WAITER_RUNNING = 2; // waiter is running
|
||||
public static final int TS_RESUMER_RUNNING = 3; // resumer is running
|
||||
public static final int TS_READY_TO_NOTIFY = 4; // ready to notify threadLock
|
||||
public static final int TS_CALL_SUSPEND = 5; // call suspend on contender
|
||||
public static final int TS_READY_TO_RESUME = 6; // ready to resume waiter
|
||||
public static final int TS_CALL_RESUME = 7; // call resume on waiter
|
||||
public static final int TS_WAITER_DONE = 8; // waiter has run; done
|
||||
|
||||
public static Object barrierLaunch = new Object(); // controls thread launch
|
||||
public static Object barrierResumer = new Object(); // controls resumer
|
||||
public static Object threadLock = new Object(); // testing object
|
||||
|
||||
public static long count = 0;
|
||||
public static boolean printDebug = false;
|
||||
public volatile static int testState;
|
||||
|
||||
private static void log(String msg) { System.out.println(msg); }
|
||||
|
||||
native static int suspendThread(SuspendWithObjectMonitorWaitWorker thr);
|
||||
native static int wait4ContendedEnter(SuspendWithObjectMonitorWaitWorker thr);
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
try {
|
||||
System.loadLibrary(AGENT_LIB);
|
||||
log("Loaded library: " + AGENT_LIB);
|
||||
} catch (UnsatisfiedLinkError ule) {
|
||||
log("Failed to load library: " + AGENT_LIB);
|
||||
log("java.library.path: " + System.getProperty("java.library.path"));
|
||||
throw ule;
|
||||
}
|
||||
|
||||
int timeMax = 0;
|
||||
if (args.length == 0) {
|
||||
timeMax = DEF_TIME_MAX;
|
||||
} else {
|
||||
int argIndex = 0;
|
||||
int argsLeft = args.length;
|
||||
if (args[0].equals("-p")) {
|
||||
printDebug = true;
|
||||
argIndex = 1;
|
||||
argsLeft--;
|
||||
}
|
||||
if (argsLeft == 0) {
|
||||
timeMax = DEF_TIME_MAX;
|
||||
} else if (argsLeft == 1) {
|
||||
try {
|
||||
timeMax = Integer.parseUnsignedInt(args[argIndex]);
|
||||
} catch (NumberFormatException nfe) {
|
||||
System.err.println("'" + args[argIndex] +
|
||||
"': invalid timeMax value.");
|
||||
usage();
|
||||
}
|
||||
} else {
|
||||
usage();
|
||||
}
|
||||
}
|
||||
|
||||
System.exit(run(timeMax, System.out) + exit_delta);
|
||||
}
|
||||
|
||||
public static void logDebug(String mesg) {
|
||||
if (printDebug) {
|
||||
System.err.println(Thread.currentThread().getName() + ": " + mesg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void usage() {
|
||||
System.err.println("Usage: " + AGENT_LIB + " [-p][time_max]");
|
||||
System.err.println("where:");
|
||||
System.err.println(" -p ::= print debug info");
|
||||
System.err.println(" time_max ::= max looping time in seconds");
|
||||
System.err.println(" (default is " + DEF_TIME_MAX +
|
||||
" seconds)");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
public static int run(int timeMax, PrintStream out) {
|
||||
return (new SuspendWithObjectMonitorWait()).doWork(timeMax, out);
|
||||
}
|
||||
|
||||
public static void checkTestState(int exp) {
|
||||
if (testState != exp) {
|
||||
System.err.println("Failure at " + count + " loops.");
|
||||
throw new InternalError("Unexpected test state value: "
|
||||
+ "expected=" + exp + " actual=" + testState);
|
||||
}
|
||||
}
|
||||
|
||||
public int doWork(int timeMax, PrintStream out) {
|
||||
SuspendWithObjectMonitorWaitWorker waiter; // waiter thread
|
||||
SuspendWithObjectMonitorWaitWorker resumer; // resumer thread
|
||||
|
||||
System.out.println("About to execute for " + timeMax + " seconds.");
|
||||
|
||||
long start_time = System.currentTimeMillis();
|
||||
while (System.currentTimeMillis() < start_time + (timeMax * 1000)) {
|
||||
count++;
|
||||
testState = TS_INIT; // starting the test loop
|
||||
|
||||
// launch the waiter thread
|
||||
synchronized (barrierLaunch) {
|
||||
waiter = new SuspendWithObjectMonitorWaitWorker("waiter");
|
||||
waiter.start();
|
||||
|
||||
while (testState != TS_WAITER_RUNNING) {
|
||||
try {
|
||||
barrierLaunch.wait(0); // wait until it is running
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// launch the resumer thread
|
||||
synchronized (barrierLaunch) {
|
||||
resumer = new SuspendWithObjectMonitorWaitWorker("resumer", waiter);
|
||||
resumer.start();
|
||||
|
||||
while (testState != TS_RESUMER_RUNNING) {
|
||||
try {
|
||||
barrierLaunch.wait(0); // wait until it is running
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
checkTestState(TS_RESUMER_RUNNING);
|
||||
|
||||
// The waiter thread was synchronized on threadLock before it
|
||||
// set TS_WAITER_RUNNING and notified barrierLaunch above so
|
||||
// we cannot enter threadLock until the waiter thread calls
|
||||
// threadLock.wait().
|
||||
synchronized (threadLock) {
|
||||
// notify waiter thread so it can try to reenter threadLock
|
||||
testState = TS_READY_TO_NOTIFY;
|
||||
threadLock.notify();
|
||||
|
||||
// wait for the waiter thread to block
|
||||
logDebug("before contended enter wait");
|
||||
int retCode = wait4ContendedEnter(waiter);
|
||||
if (retCode != 0) {
|
||||
throw new RuntimeException("error in JVMTI GetThreadState: "
|
||||
+ "retCode=" + retCode);
|
||||
}
|
||||
logDebug("done contended enter wait");
|
||||
|
||||
checkTestState(TS_READY_TO_NOTIFY);
|
||||
testState = TS_CALL_SUSPEND;
|
||||
logDebug("before suspend thread");
|
||||
retCode = suspendThread(waiter);
|
||||
if (retCode != 0) {
|
||||
throw new RuntimeException("error in JVMTI SuspendThread: "
|
||||
+ "retCode=" + retCode);
|
||||
}
|
||||
logDebug("suspended thread");
|
||||
}
|
||||
|
||||
//
|
||||
// At this point, all of the child threads are running
|
||||
// and we can get to meat of the test:
|
||||
//
|
||||
// - suspended threadLock waiter (trying to reenter)
|
||||
// - a threadLock enter in the resumer thread
|
||||
// - resumption of the waiter thread
|
||||
// - a threadLock enter in the freshly resumed waiter thread
|
||||
//
|
||||
|
||||
synchronized (barrierResumer) {
|
||||
checkTestState(TS_CALL_SUSPEND);
|
||||
|
||||
// tell resumer thread to resume waiter thread
|
||||
testState = TS_READY_TO_RESUME;
|
||||
barrierResumer.notify();
|
||||
|
||||
// Can't call checkTestState() here because the
|
||||
// resumer thread may have already resumed the
|
||||
// waiter thread.
|
||||
}
|
||||
|
||||
try {
|
||||
resumer.join(JOIN_MAX * 1000);
|
||||
if (resumer.isAlive()) {
|
||||
System.err.println("Failure at " + count + " loops.");
|
||||
throw new InternalError("resumer thread is stuck");
|
||||
}
|
||||
waiter.join(JOIN_MAX * 1000);
|
||||
if (waiter.isAlive()) {
|
||||
System.err.println("Failure at " + count + " loops.");
|
||||
throw new InternalError("waiter thread is stuck");
|
||||
}
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
|
||||
checkTestState(TS_WAITER_DONE);
|
||||
}
|
||||
|
||||
System.out.println("Executed " + count + " loops in " + timeMax +
|
||||
" seconds.");
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
class SuspendWithObjectMonitorWaitWorker extends Thread {
|
||||
private SuspendWithObjectMonitorWaitWorker target; // target for resume operation
|
||||
|
||||
public SuspendWithObjectMonitorWaitWorker(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public SuspendWithObjectMonitorWaitWorker(String name, SuspendWithObjectMonitorWaitWorker target) {
|
||||
super(name);
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
native static int resumeThread(SuspendWithObjectMonitorWaitWorker thr);
|
||||
|
||||
public void run() {
|
||||
SuspendWithObjectMonitorWait.logDebug("thread running");
|
||||
|
||||
//
|
||||
// Launch the waiter thread:
|
||||
// - grab the threadLock
|
||||
// - threadLock.wait()
|
||||
// - releases threadLock
|
||||
//
|
||||
if (getName().equals("waiter")) {
|
||||
// grab threadLock before we tell main we are running
|
||||
SuspendWithObjectMonitorWait.logDebug("before enter threadLock");
|
||||
synchronized(SuspendWithObjectMonitorWait.threadLock) {
|
||||
SuspendWithObjectMonitorWait.logDebug("enter threadLock");
|
||||
|
||||
SuspendWithObjectMonitorWait.checkTestState(SuspendWithObjectMonitorWait.TS_INIT);
|
||||
|
||||
synchronized(SuspendWithObjectMonitorWait.barrierLaunch) {
|
||||
// tell main we are running
|
||||
SuspendWithObjectMonitorWait.testState = SuspendWithObjectMonitorWait.TS_WAITER_RUNNING;
|
||||
SuspendWithObjectMonitorWait.barrierLaunch.notify();
|
||||
}
|
||||
|
||||
SuspendWithObjectMonitorWait.logDebug("before wait");
|
||||
|
||||
// TS_READY_TO_NOTIFY is set after the main thread has
|
||||
// entered threadLock so a spurious wakeup can't get the
|
||||
// waiter thread out of this threadLock.wait(0) call:
|
||||
while (SuspendWithObjectMonitorWait.testState <= SuspendWithObjectMonitorWait.TS_READY_TO_NOTIFY) {
|
||||
try {
|
||||
SuspendWithObjectMonitorWait.threadLock.wait(0);
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
}
|
||||
|
||||
SuspendWithObjectMonitorWait.logDebug("after wait");
|
||||
|
||||
SuspendWithObjectMonitorWait.checkTestState(SuspendWithObjectMonitorWait.TS_CALL_RESUME);
|
||||
SuspendWithObjectMonitorWait.testState = SuspendWithObjectMonitorWait.TS_WAITER_DONE;
|
||||
|
||||
SuspendWithObjectMonitorWait.logDebug("exit threadLock");
|
||||
}
|
||||
}
|
||||
//
|
||||
// Launch the resumer thread:
|
||||
// - tries to grab the threadLock (should not block!)
|
||||
// - grabs threadLock
|
||||
// - resumes the waiter thread
|
||||
// - releases threadLock
|
||||
//
|
||||
else if (getName().equals("resumer")) {
|
||||
synchronized(SuspendWithObjectMonitorWait.barrierResumer) {
|
||||
synchronized(SuspendWithObjectMonitorWait.barrierLaunch) {
|
||||
// tell main we are running
|
||||
SuspendWithObjectMonitorWait.testState = SuspendWithObjectMonitorWait.TS_RESUMER_RUNNING;
|
||||
SuspendWithObjectMonitorWait.barrierLaunch.notify();
|
||||
}
|
||||
SuspendWithObjectMonitorWait.logDebug("thread waiting");
|
||||
while (SuspendWithObjectMonitorWait.testState != SuspendWithObjectMonitorWait.TS_READY_TO_RESUME) {
|
||||
try {
|
||||
// wait for main to tell us when to continue
|
||||
SuspendWithObjectMonitorWait.barrierResumer.wait(0);
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SuspendWithObjectMonitorWait.logDebug("before enter threadLock");
|
||||
synchronized(SuspendWithObjectMonitorWait.threadLock) {
|
||||
SuspendWithObjectMonitorWait.logDebug("enter threadLock");
|
||||
|
||||
SuspendWithObjectMonitorWait.checkTestState(SuspendWithObjectMonitorWait.TS_READY_TO_RESUME);
|
||||
SuspendWithObjectMonitorWait.testState = SuspendWithObjectMonitorWait.TS_CALL_RESUME;
|
||||
|
||||
// resume the waiter thread so waiter.join() can work
|
||||
SuspendWithObjectMonitorWait.logDebug("before resume thread");
|
||||
int retCode = resumeThread(target);
|
||||
if (retCode != 0) {
|
||||
throw new RuntimeException("error in JVMTI ResumeThread: " +
|
||||
"retCode=" + retCode);
|
||||
}
|
||||
SuspendWithObjectMonitorWait.logDebug("resumed thread");
|
||||
|
||||
SuspendWithObjectMonitorWait.logDebug("exit threadLock");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "jvmti.h"
|
||||
|
||||
extern "C" {
|
||||
|
||||
static jvmtiEnv* jvmti = NULL;
|
||||
|
||||
#define LOG(...) \
|
||||
do { \
|
||||
printf(__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
fflush(stdout); \
|
||||
} while (0)
|
||||
|
||||
JNIEXPORT int JNICALL
|
||||
Java_SuspendWithObjectMonitorWait_suspendThread(JNIEnv *jni, jclass cls, jthread thr) {
|
||||
return jvmti->SuspendThread(thr);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_SuspendWithObjectMonitorWaitWorker_resumeThread(JNIEnv *jni, jclass cls, jthread thr) {
|
||||
return jvmti->ResumeThread(thr);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_SuspendWithObjectMonitorWait_wait4ContendedEnter(JNIEnv *jni, jclass cls, jthread thr) {
|
||||
jvmtiError err;
|
||||
jint thread_state;
|
||||
do {
|
||||
err = jvmti->GetThreadState(thr, &thread_state);
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
return err;
|
||||
}
|
||||
} while ((thread_state & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) == 0);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
/** Agent library initialization. */
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
|
||||
LOG("\nAgent_OnLoad started");
|
||||
|
||||
// create JVMTI environment
|
||||
if (jvm->GetEnv((void **) (&jvmti), JVMTI_VERSION) != JNI_OK) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
// add specific capabilities for suspending thread
|
||||
jvmtiCapabilities suspendCaps;
|
||||
memset(&suspendCaps, 0, sizeof(suspendCaps));
|
||||
suspendCaps.can_suspend = 1;
|
||||
|
||||
jvmtiError err = jvmti->AddCapabilities(&suspendCaps);
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
LOG("Agent_OnLoad finished\n");
|
||||
return JNI_OK;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,464 @@
|
||||
/*
|
||||
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4413752
|
||||
* @summary Test SuspendThread with RawMonitor enter.
|
||||
* @requires vm.jvmti
|
||||
* @library /test/lib
|
||||
* @compile SuspendWithRawMonitorEnter.java
|
||||
* @run main/othervm/native -agentlib:SuspendWithRawMonitorEnter SuspendWithRawMonitorEnter
|
||||
*/
|
||||
|
||||
import java.io.PrintStream;
|
||||
|
||||
//
|
||||
// main blocker contender resumer
|
||||
// ================= ================ =================== ================
|
||||
// launch blocker
|
||||
// <launch returns> blocker running
|
||||
// launch contender enter threadLock
|
||||
// <launch returns> wait for notify contender running
|
||||
// launch resumer : block on threadLock
|
||||
// <launch returns> : : resumer running
|
||||
// suspend contender : <suspended> wait for notify
|
||||
// <ready to test> : : :
|
||||
// : : : :
|
||||
// notify blocker wait finishes : :
|
||||
// notify resumer exit threadLock : wait finishes
|
||||
// join blocker : : enter threadLock
|
||||
// <join returns> blocker exits <resumed> resume contender
|
||||
// join resumer : exit threadLock
|
||||
// <join returns> enter threadLock resumer exits
|
||||
// join contender exit threadLock
|
||||
// <join returns> contender exits
|
||||
//
|
||||
|
||||
public class SuspendWithRawMonitorEnter {
|
||||
private static final String AGENT_LIB = "SuspendWithRawMonitorEnter";
|
||||
private static final int exit_delta = 95;
|
||||
|
||||
private static final int DEF_TIME_MAX = 60; // default max # secs to test
|
||||
private static final int JOIN_MAX = 30; // max # secs to wait for join
|
||||
|
||||
public static final int TS_INIT = 1; // initial testState
|
||||
public static final int TS_BLOCKER_RUNNING = 2; // blocker is running
|
||||
public static final int TS_CONTENDER_RUNNING = 3; // contender is running
|
||||
public static final int TS_RESUMER_RUNNING = 4; // resumer is running
|
||||
public static final int TS_CALL_SUSPEND = 5; // call suspend on contender
|
||||
public static final int TS_DONE_BLOCKING = 6; // done blocking threadLock
|
||||
public static final int TS_READY_TO_RESUME = 7; // ready to resume contender
|
||||
public static final int TS_CALL_RESUME = 8; // call resume on contender
|
||||
public static final int TS_CONTENDER_DONE = 9; // contender has run; done
|
||||
|
||||
public static Object barrierLaunch = new Object(); // controls thread launch
|
||||
public static Object barrierBlocker = new Object(); // controls blocker
|
||||
public static Object barrierResumer = new Object(); // controls resumer
|
||||
|
||||
public static long count = 0;
|
||||
public static boolean printDebug = false;
|
||||
public volatile static int testState;
|
||||
|
||||
private static void log(String msg) { System.out.println(msg); }
|
||||
|
||||
native static int createRawMonitor();
|
||||
native static int destroyRawMonitor();
|
||||
native static int suspendThread(SuspendWithRawMonitorEnterWorker thr);
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
try {
|
||||
System.loadLibrary(AGENT_LIB);
|
||||
log("Loaded library: " + AGENT_LIB);
|
||||
} catch (UnsatisfiedLinkError ule) {
|
||||
log("Failed to load library: " + AGENT_LIB);
|
||||
log("java.library.path: " + System.getProperty("java.library.path"));
|
||||
throw ule;
|
||||
}
|
||||
|
||||
int timeMax = 0;
|
||||
if (args.length == 0) {
|
||||
timeMax = DEF_TIME_MAX;
|
||||
} else {
|
||||
int argIndex = 0;
|
||||
int argsLeft = args.length;
|
||||
if (args[0].equals("-p")) {
|
||||
printDebug = true;
|
||||
argIndex = 1;
|
||||
argsLeft--;
|
||||
}
|
||||
if (argsLeft == 0) {
|
||||
timeMax = DEF_TIME_MAX;
|
||||
} else if (argsLeft == 1) {
|
||||
try {
|
||||
timeMax = Integer.parseUnsignedInt(args[argIndex]);
|
||||
} catch (NumberFormatException nfe) {
|
||||
System.err.println("'" + args[argIndex] +
|
||||
"': invalid timeMax value.");
|
||||
usage();
|
||||
}
|
||||
} else {
|
||||
usage();
|
||||
}
|
||||
}
|
||||
|
||||
System.exit(run(timeMax, System.out) + exit_delta);
|
||||
}
|
||||
|
||||
public static void logDebug(String mesg) {
|
||||
if (printDebug) {
|
||||
System.err.println(Thread.currentThread().getName() + ": " + mesg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void usage() {
|
||||
System.err.println("Usage: " + AGENT_LIB + " [-p][time_max]");
|
||||
System.err.println("where:");
|
||||
System.err.println(" -p ::= print debug info");
|
||||
System.err.println(" time_max ::= max looping time in seconds");
|
||||
System.err.println(" (default is " + DEF_TIME_MAX +
|
||||
" seconds)");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
public static int run(int timeMax, PrintStream out) {
|
||||
return (new SuspendWithRawMonitorEnter()).doWork(timeMax, out);
|
||||
}
|
||||
|
||||
public static void checkTestState(int exp) {
|
||||
if (testState != exp) {
|
||||
System.err.println("Failure at " + count + " loops.");
|
||||
throw new InternalError("Unexpected test state value: "
|
||||
+ "expected=" + exp + " actual=" + testState);
|
||||
}
|
||||
}
|
||||
|
||||
public int doWork(int timeMax, PrintStream out) {
|
||||
SuspendWithRawMonitorEnterWorker blocker; // blocker thread
|
||||
SuspendWithRawMonitorEnterWorker contender; // contender thread
|
||||
SuspendWithRawMonitorEnterWorker resumer; // resumer thread
|
||||
|
||||
int retCode = createRawMonitor();
|
||||
if (retCode != 0) {
|
||||
throw new RuntimeException("error in JVMTI CreateRawMonitor: " +
|
||||
"retCode=" + retCode);
|
||||
}
|
||||
logDebug("created threadLock");
|
||||
|
||||
System.out.println("About to execute for " + timeMax + " seconds.");
|
||||
|
||||
long start_time = System.currentTimeMillis();
|
||||
while (System.currentTimeMillis() < start_time + (timeMax * 1000)) {
|
||||
count++;
|
||||
testState = TS_INIT; // starting the test loop
|
||||
|
||||
// launch the blocker thread
|
||||
synchronized (barrierLaunch) {
|
||||
blocker = new SuspendWithRawMonitorEnterWorker("blocker");
|
||||
blocker.start();
|
||||
|
||||
while (testState != TS_BLOCKER_RUNNING) {
|
||||
try {
|
||||
barrierLaunch.wait(0); // wait until it is running
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// launch the contender thread
|
||||
synchronized (barrierLaunch) {
|
||||
contender = new SuspendWithRawMonitorEnterWorker("contender");
|
||||
contender.start();
|
||||
|
||||
while (testState != TS_CONTENDER_RUNNING) {
|
||||
try {
|
||||
barrierLaunch.wait(0); // wait until it is running
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// launch the resumer thread
|
||||
synchronized (barrierLaunch) {
|
||||
resumer = new SuspendWithRawMonitorEnterWorker("resumer", contender);
|
||||
resumer.start();
|
||||
|
||||
while (testState != TS_RESUMER_RUNNING) {
|
||||
try {
|
||||
barrierLaunch.wait(0); // wait until it is running
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Known bug: We don't have a way of knowing when the
|
||||
// contender thread contends on the threadLock. If we
|
||||
// suspend it before it has blocked, then we don't really
|
||||
// have contention. However, the resumer thread won't
|
||||
// resume the contender thread until after it has grabbed
|
||||
// the threadLock so we don't have a lock order problem
|
||||
// and the test won't fall over.
|
||||
//
|
||||
// We reduce the size of this timing window by launching
|
||||
// the resumer thread after the contender thread. So the
|
||||
// contender thread has all the setup time for the resumer
|
||||
// thread to call JVM/TI RawMonitorEnter() and block on
|
||||
// the threadLock.
|
||||
//
|
||||
checkTestState(TS_RESUMER_RUNNING);
|
||||
testState = TS_CALL_SUSPEND;
|
||||
logDebug("before suspend thread");
|
||||
retCode = suspendThread(contender);
|
||||
if (retCode != 0) {
|
||||
throw new RuntimeException("error in JVMTI SuspendThread: " +
|
||||
"retCode=" + retCode);
|
||||
}
|
||||
logDebug("suspended thread");
|
||||
|
||||
//
|
||||
// At this point, all of the child threads are running
|
||||
// and we can get to meat of the test:
|
||||
//
|
||||
// - suspended threadLock contender
|
||||
// - a threadLock exit in the blocker thread
|
||||
// - a threadLock enter in the resumer thread
|
||||
// - resumption of the contender thread
|
||||
// - a threadLock enter in the freshly resumed contender thread
|
||||
//
|
||||
synchronized (barrierBlocker) {
|
||||
checkTestState(TS_CALL_SUSPEND);
|
||||
|
||||
// tell blocker thread to exit threadLock
|
||||
testState = TS_DONE_BLOCKING;
|
||||
barrierBlocker.notify();
|
||||
}
|
||||
|
||||
synchronized (barrierResumer) {
|
||||
// tell resumer thread to resume contender thread
|
||||
testState = TS_READY_TO_RESUME;
|
||||
barrierResumer.notify();
|
||||
|
||||
// Can't call checkTestState() here because the
|
||||
// resumer thread may have already resumed the
|
||||
// contender thread.
|
||||
}
|
||||
|
||||
try {
|
||||
blocker.join();
|
||||
resumer.join(JOIN_MAX * 1000);
|
||||
if (resumer.isAlive()) {
|
||||
System.err.println("Failure at " + count + " loops.");
|
||||
throw new InternalError("resumer thread is stuck");
|
||||
}
|
||||
contender.join(JOIN_MAX * 1000);
|
||||
if (contender.isAlive()) {
|
||||
System.err.println("Failure at " + count + " loops.");
|
||||
throw new InternalError("contender thread is stuck");
|
||||
}
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
|
||||
checkTestState(TS_CONTENDER_DONE);
|
||||
}
|
||||
retCode = destroyRawMonitor();
|
||||
if (retCode != 0) {
|
||||
throw new RuntimeException("error in JVMTI DestroyRawMonitor: " +
|
||||
"retCode=" + retCode);
|
||||
}
|
||||
logDebug("destroyed threadLock");
|
||||
|
||||
System.out.println("Executed " + count + " loops in " + timeMax +
|
||||
" seconds.");
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
class SuspendWithRawMonitorEnterWorker extends Thread {
|
||||
private SuspendWithRawMonitorEnterWorker target; // target for resume operation
|
||||
|
||||
public SuspendWithRawMonitorEnterWorker(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public SuspendWithRawMonitorEnterWorker(String name, SuspendWithRawMonitorEnterWorker target) {
|
||||
super(name);
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
native static int rawMonitorEnter();
|
||||
native static int rawMonitorExit();
|
||||
native static int resumeThread(SuspendWithRawMonitorEnterWorker thr);
|
||||
|
||||
public void run() {
|
||||
SuspendWithRawMonitorEnter.logDebug("thread running");
|
||||
|
||||
//
|
||||
// Launch the blocker thread:
|
||||
// - grabs threadLock
|
||||
// - holds threadLock until we tell it let go
|
||||
// - releases threadLock
|
||||
//
|
||||
int retCode;
|
||||
if (getName().equals("blocker")) {
|
||||
// grab threadLock before we tell main we are running
|
||||
SuspendWithRawMonitorEnter.logDebug("before enter threadLock");
|
||||
retCode = rawMonitorEnter();
|
||||
if (retCode != 0) {
|
||||
throw new RuntimeException("error in JVMTI RawMonitorEnter: " +
|
||||
"retCode=" + retCode);
|
||||
}
|
||||
SuspendWithRawMonitorEnter.logDebug("enter threadLock");
|
||||
|
||||
SuspendWithRawMonitorEnter.checkTestState(SuspendWithRawMonitorEnter.TS_INIT);
|
||||
|
||||
// recursive entry
|
||||
SuspendWithRawMonitorEnter.logDebug("before recursive enter threadLock");
|
||||
retCode = rawMonitorEnter();
|
||||
if (retCode != 0) {
|
||||
throw new RuntimeException("error in JVMTI RawMonitorEnter: " +
|
||||
"retCode=" + retCode);
|
||||
}
|
||||
SuspendWithRawMonitorEnter.logDebug("recursive enter threadLock");
|
||||
|
||||
SuspendWithRawMonitorEnter.logDebug("before recursive exit threadLock");
|
||||
retCode = rawMonitorExit();
|
||||
if (retCode != 0) {
|
||||
throw new RuntimeException("error in JVMTI RawMonitorExit: " +
|
||||
"retCode=" + retCode);
|
||||
}
|
||||
SuspendWithRawMonitorEnter.logDebug("recursive exit threadLock");
|
||||
|
||||
synchronized(SuspendWithRawMonitorEnter.barrierBlocker) {
|
||||
synchronized(SuspendWithRawMonitorEnter.barrierLaunch) {
|
||||
// tell main we are running
|
||||
SuspendWithRawMonitorEnter.testState = SuspendWithRawMonitorEnter.TS_BLOCKER_RUNNING;
|
||||
SuspendWithRawMonitorEnter.barrierLaunch.notify();
|
||||
}
|
||||
SuspendWithRawMonitorEnter.logDebug("thread waiting");
|
||||
// TS_READY_TO_RESUME is set right after TS_DONE_BLOCKING
|
||||
// is set so either can get the blocker thread out of
|
||||
// this wait() wrapper:
|
||||
while (SuspendWithRawMonitorEnter.testState != SuspendWithRawMonitorEnter.TS_DONE_BLOCKING &&
|
||||
SuspendWithRawMonitorEnter.testState != SuspendWithRawMonitorEnter.TS_READY_TO_RESUME) {
|
||||
try {
|
||||
// wait for main to tell us when to exit threadLock
|
||||
SuspendWithRawMonitorEnter.barrierBlocker.wait(0);
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
}
|
||||
SuspendWithRawMonitorEnter.logDebug("before exit threadLock");
|
||||
retCode = rawMonitorExit();
|
||||
if (retCode != 0) {
|
||||
throw new RuntimeException("error in JVMTI RawMonitorExit: "
|
||||
+ "retCode=" + retCode);
|
||||
}
|
||||
SuspendWithRawMonitorEnter.logDebug("exit threadLock");
|
||||
}
|
||||
}
|
||||
//
|
||||
// Launch the contender thread:
|
||||
// - tries to grab the threadLock
|
||||
// - grabs threadLock
|
||||
// - releases threadLock
|
||||
//
|
||||
else if (getName().equals("contender")) {
|
||||
synchronized(SuspendWithRawMonitorEnter.barrierLaunch) {
|
||||
// tell main we are running
|
||||
SuspendWithRawMonitorEnter.testState = SuspendWithRawMonitorEnter.TS_CONTENDER_RUNNING;
|
||||
SuspendWithRawMonitorEnter.barrierLaunch.notify();
|
||||
}
|
||||
|
||||
SuspendWithRawMonitorEnter.logDebug("before enter threadLock");
|
||||
retCode = rawMonitorEnter();
|
||||
if (retCode != 0) {
|
||||
throw new RuntimeException("error in JVMTI RawMonitorEnter: " +
|
||||
"retCode=" + retCode);
|
||||
}
|
||||
SuspendWithRawMonitorEnter.logDebug("enter threadLock");
|
||||
|
||||
SuspendWithRawMonitorEnter.checkTestState(SuspendWithRawMonitorEnter.TS_CALL_RESUME);
|
||||
SuspendWithRawMonitorEnter.testState = SuspendWithRawMonitorEnter.TS_CONTENDER_DONE;
|
||||
|
||||
SuspendWithRawMonitorEnter.logDebug("before exit threadLock");
|
||||
retCode = rawMonitorExit();
|
||||
if (retCode != 0) {
|
||||
throw new RuntimeException("error in JVMTI RawMonitorExit: " +
|
||||
"retCode=" + retCode);
|
||||
}
|
||||
SuspendWithRawMonitorEnter.logDebug("exit threadLock");
|
||||
}
|
||||
//
|
||||
// Launch the resumer thread:
|
||||
// - tries to grab the threadLock (should not block!)
|
||||
// - grabs threadLock
|
||||
// - resumes the contended thread
|
||||
// - releases threadLock
|
||||
//
|
||||
else if (getName().equals("resumer")) {
|
||||
synchronized(SuspendWithRawMonitorEnter.barrierResumer) {
|
||||
synchronized(SuspendWithRawMonitorEnter.barrierLaunch) {
|
||||
// tell main we are running
|
||||
SuspendWithRawMonitorEnter.testState = SuspendWithRawMonitorEnter.TS_RESUMER_RUNNING;
|
||||
SuspendWithRawMonitorEnter.barrierLaunch.notify();
|
||||
}
|
||||
SuspendWithRawMonitorEnter.logDebug("thread waiting");
|
||||
while (SuspendWithRawMonitorEnter.testState != SuspendWithRawMonitorEnter.TS_READY_TO_RESUME) {
|
||||
try {
|
||||
// wait for main to tell us when to continue
|
||||
SuspendWithRawMonitorEnter.barrierResumer.wait(0);
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
SuspendWithRawMonitorEnter.logDebug("before enter threadLock");
|
||||
retCode = rawMonitorEnter();
|
||||
if (retCode != 0) {
|
||||
throw new RuntimeException("error in JVMTI RawMonitorEnter: " +
|
||||
"retCode=" + retCode);
|
||||
}
|
||||
SuspendWithRawMonitorEnter.logDebug("enter threadLock");
|
||||
|
||||
SuspendWithRawMonitorEnter.checkTestState(SuspendWithRawMonitorEnter.TS_READY_TO_RESUME);
|
||||
SuspendWithRawMonitorEnter.testState = SuspendWithRawMonitorEnter.TS_CALL_RESUME;
|
||||
|
||||
// resume the contender thread so contender.join() can work
|
||||
SuspendWithRawMonitorEnter.logDebug("before resume thread");
|
||||
retCode = resumeThread(target);
|
||||
if (retCode != 0) {
|
||||
throw new RuntimeException("error in JVMTI ResumeThread: " +
|
||||
"retCode=" + retCode);
|
||||
}
|
||||
SuspendWithRawMonitorEnter.logDebug("resumed thread");
|
||||
|
||||
SuspendWithRawMonitorEnter.logDebug("before exit threadLock");
|
||||
retCode = rawMonitorExit();
|
||||
if (retCode != 0) {
|
||||
throw new RuntimeException("error in JVMTI RawMonitorExit: " +
|
||||
"retCode=" + retCode);
|
||||
}
|
||||
SuspendWithRawMonitorEnter.logDebug("exit threadLock");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "jvmti.h"
|
||||
|
||||
extern "C" {
|
||||
|
||||
static jvmtiEnv* jvmti = NULL;
|
||||
static jrawMonitorID threadLock = NULL;
|
||||
static char threadLockName[] = "threadLock";
|
||||
|
||||
#define LOG(...) \
|
||||
do { \
|
||||
printf(__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
fflush(stdout); \
|
||||
} while (0)
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_SuspendWithRawMonitorEnter_createRawMonitor(JNIEnv *jni, jclass cls) {
|
||||
return jvmti->CreateRawMonitor(threadLockName, &threadLock);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_SuspendWithRawMonitorEnter_destroyRawMonitor(JNIEnv *jni, jclass cls) {
|
||||
return jvmti->DestroyRawMonitor(threadLock);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_SuspendWithRawMonitorEnter_suspendThread(JNIEnv *jni, jclass cls, jthread thr) {
|
||||
return jvmti->SuspendThread(thr);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_SuspendWithRawMonitorEnterWorker_rawMonitorEnter(JNIEnv *jni, jclass cls) {
|
||||
return jvmti->RawMonitorEnter(threadLock);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_SuspendWithRawMonitorEnterWorker_rawMonitorExit(JNIEnv *jni, jclass cls) {
|
||||
return jvmti->RawMonitorExit(threadLock);
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Java_SuspendWithRawMonitorEnterWorker_resumeThread(JNIEnv *jni, jclass cls, jthread thr) {
|
||||
return jvmti->ResumeThread(thr);
|
||||
}
|
||||
|
||||
|
||||
/** Agent library initialization. */
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
|
||||
LOG("\nAgent_OnLoad started");
|
||||
|
||||
// create JVMTI environment
|
||||
if (jvm->GetEnv((void **) (&jvmti), JVMTI_VERSION) != JNI_OK) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
// add specific capabilities for suspending thread
|
||||
jvmtiCapabilities suspendCaps;
|
||||
memset(&suspendCaps, 0, sizeof(suspendCaps));
|
||||
suspendCaps.can_suspend = 1;
|
||||
|
||||
jvmtiError err = jvmti->AddCapabilities(&suspendCaps);
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
LOG("Agent_OnLoad finished\n");
|
||||
return JNI_OK;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user