Skip to content

Commit

Permalink
8265240: runtime/Thread/SuspendAtExit.java needs updating
Browse files Browse the repository at this point in the history
Reviewed-by: rehn, dholmes
  • Loading branch information
Daniel D. Daugherty committed Apr 23, 2021
1 parent 891f72f commit c9b70c8
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 42 deletions.
143 changes: 101 additions & 42 deletions test/hotspot/jtreg/runtime/Thread/SuspendAtExit.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 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
Expand All @@ -23,88 +23,147 @@

/**
* @test
* @bug 8167108
* @summary Stress test java.lang.Thread.suspend() at thread exit.
* @run main/othervm -Xlog:thread+smr=debug SuspendAtExit
* @run main/othervm -Xlog:thread+smr=debug -XX:+UnlockDiagnosticVMOptions -XX:GuaranteedSafepointInterval=1 -XX:+HandshakeALot SuspendAtExit
* @bug 8167108 8265240
* @summary Stress test SuspendThread at thread exit.
* @requires vm.jvmti
* @run main/othervm/native -agentlib:SuspendAtExit SuspendAtExit
* @run main/othervm/native -agentlib:SuspendAtExit -XX:+UnlockDiagnosticVMOptions -XX:GuaranteedSafepointInterval=1 -XX:+HandshakeALot SuspendAtExit
*/

import java.util.concurrent.CountDownLatch;

public class SuspendAtExit extends Thread {
final static int N_THREADS = 32;
final static int N_LATE_CALLS = 10000;
private final static String AGENT_LIB = "SuspendAtExit";
private final static int DEF_TIME_MAX = 30; // default max # secs to test
private final static int JVMTI_ERROR_THREAD_NOT_ALIVE = 15;

public CountDownLatch exitSyncObj = new CountDownLatch(1);
public CountDownLatch startSyncObj = new CountDownLatch(1);

private static void log(String msg) { System.out.println(msg); }

native static int resumeThread(SuspendAtExit thr);
native static int suspendThread(SuspendAtExit thr);

@Override
public void run() {
// Tell main thread we have started.
startSyncObj.countDown();
try {
// Wait for main thread to interrupt us so we
// can race to exit.
// Wait for main thread to tell us to race to the exit.
exitSyncObj.await();
} catch (InterruptedException e) {
// ignore because we expect one
throw new RuntimeException("Unexpected: " + e);
}
}

public static void main(String[] args) {
SuspendAtExit threads[] = new SuspendAtExit[N_THREADS];
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;
}

for (int i = 0; i < N_THREADS; i++ ) {
threads[i] = new SuspendAtExit();
int late_count = 1;
threads[i].start();
int timeMax = 0;
if (args.length == 0) {
timeMax = DEF_TIME_MAX;
} else {
try {
// Wait for the worker thread to get going.
threads[i].startSyncObj.await();
timeMax = Integer.parseUnsignedInt(args[0]);
} catch (NumberFormatException nfe) {
System.err.println("'" + args[0] + "': invalid timeMax value.");
usage();
}
}

System.out.println("About to execute for " + timeMax + " seconds.");

// This interrupt() call will break the worker out
// of the exitSyncObj.await() call and the suspend()
// calls will come in during thread exit.
threads[i].interrupt();
for (; late_count <= N_LATE_CALLS; late_count++) {
threads[i].suspend();
long count = 0;
long start_time = System.currentTimeMillis();
while (System.currentTimeMillis() < start_time + (timeMax * 1000)) {
count++;

if (!threads[i].isAlive()) {
// Done with Thread.suspend() calls since
int retCode;
SuspendAtExit thread = new SuspendAtExit();
thread.start();
try {
// Wait for the worker thread to get going.
thread.startSyncObj.await();
// Tell the worker thread to race to the exit and the
// SuspendThread() calls will come in during thread exit.
thread.exitSyncObj.countDown();
while (true) {
retCode = suspendThread(thread);

if (retCode == JVMTI_ERROR_THREAD_NOT_ALIVE) {
// Done with SuspendThread() calls since
// thread is not alive.
break;
} else if (retCode != 0) {
throw new RuntimeException("thread " + thread.getName()
+ ": suspendThread() " +
"retCode=" + retCode +
": unexpected value.");
}

if (!thread.isAlive()) {
throw new RuntimeException("thread " + thread.getName()
+ ": is not alive " +
"after successful " +
"suspendThread().");
}
retCode = resumeThread(thread);
if (retCode != 0) {
throw new RuntimeException("thread " + thread.getName()
+ ": resumeThread() " +
"retCode=" + retCode +
": unexpected value.");
}
threads[i].resume();
}
} catch (InterruptedException e) {
throw new Error("Unexpected: " + e);
throw new RuntimeException("Unexpected: " + e);
}

System.out.println("INFO: thread #" + i + ": made " + late_count +
" late calls to java.lang.Thread.suspend()");
System.out.println("INFO: thread #" + i + ": N_LATE_CALLS==" +
N_LATE_CALLS + " value is " +
((late_count >= N_LATE_CALLS) ? "NOT " : "") +
"large enough to cause a Thread.suspend() " +
"call after thread exit.");

try {
threads[i].join();
thread.join();
} catch (InterruptedException e) {
throw new Error("Unexpected: " + e);
throw new RuntimeException("Unexpected: " + e);
}
threads[i].suspend();
threads[i].resume();
if (threads[i].isAlive()) {
throw new Error("Expected !Thread.isAlive() after thread #" +
i + " has been join()'ed");
retCode = suspendThread(thread);
if (retCode != JVMTI_ERROR_THREAD_NOT_ALIVE) {
throw new RuntimeException("thread " + thread.getName() +
": suspendThread() " +
"retCode=" + retCode +
": unexpected value.");
}
retCode = resumeThread(thread);
if (retCode != JVMTI_ERROR_THREAD_NOT_ALIVE) {
throw new RuntimeException("thread " + thread.getName() +
": suspendThread() " +
"retCode=" + retCode +
": unexpected value.");
}
}

System.out.println("Executed " + count + " loops in " + timeMax +
" seconds.");

String cmd = System.getProperty("sun.java.command");
if (cmd != null && !cmd.startsWith("com.sun.javatest.regtest.agent.MainWrapper")) {
// Exit with success in a non-JavaTest environment:
System.exit(0);
}
}

public static void usage() {
System.err.println("Usage: " + AGENT_LIB + " [time_max]");
System.err.println("where:");
System.err.println(" time_max ::= max looping time in seconds");
System.err.println(" (default is " + DEF_TIME_MAX +
" seconds)");
System.exit(1);
}
}
73 changes: 73 additions & 0 deletions test/hotspot/jtreg/runtime/Thread/libSuspendAtExit.cpp
@@ -0,0 +1,73 @@
/*
* 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_SuspendAtExit_suspendThread(JNIEnv *jni, jclass cls, jthread thr) {
return jvmti->SuspendThread(thr);
}

JNIEXPORT jint JNICALL
Java_SuspendAtExit_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;
}

}

1 comment on commit c9b70c8

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.