8224555: vmTestbase/nsk/jvmti/scenarios/contention/TC02/tc02t001/TestDescription.java failed

Improve synchronization in the test

Reviewed-by: dcubed, amenkov
This commit is contained in:
Serguei Spitsyn 2019-06-21 18:20:49 -07:00
parent 035e07b212
commit fc6de2505d
2 changed files with 103 additions and 83 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 2019, 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
@ -28,86 +28,7 @@ import java.io.PrintStream;
import nsk.share.*;
import nsk.share.jvmti.*;
// THIS TEST IS LINE NUMBER SENSITIVE
public class tc02t001 extends DebugeeClass {
// run test from command line
public static void main(String argv[]) {
argv = nsk.share.jvmti.JVMTITest.commonInit(argv);
// JCK-compatible exit
System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
}
// run test from JCK-compatible environment
public static int run(String argv[], PrintStream out) {
return new tc02t001().runIt(argv, out);
}
/* =================================================================== */
// scaffold objects
ArgumentHandler argHandler = null;
Log log = null;
int status = Consts.TEST_PASSED;
static long timeout = 0;
// tested thread
tc02t001Thread thread = null;
// run debuggee
public int runIt(String argv[], PrintStream out) {
argHandler = new ArgumentHandler(argv);
log = new Log(out, argHandler);
timeout = argHandler.getWaitTime() * 60 * 1000;
log.display("Timeout = " + timeout + " msc.");
thread = new tc02t001Thread("Debuggee Thread");
synchronized (thread.M) {
thread.start();
thread.startingBarrier.waitFor();
status = checkStatus(status);
thread.waitingBarrier1.unlock();
try {
Thread.sleep(1000); // Wait for contended "synchronized (M)"
thread.M.wait(timeout);
} catch (InterruptedException e) {
throw new Failure(e);
}
thread.waitingBarrier2.unlock();
try {
Thread.sleep(1000); // Wait for contended "synchronized (M)"
thread.M.wait(timeout);
} catch (InterruptedException e) {
throw new Failure(e);
}
thread.waitingBarrier3.unlock();
try {
Thread.sleep(1000); // Wait for contended "synchronized (M)"
thread.M.wait(timeout);
} catch (InterruptedException e) {
throw new Failure(e);
}
}
try {
thread.join(timeout);
} catch (InterruptedException e) {
throw new Failure(e);
}
log.display("Debugee finished");
status = checkStatus(status);
return status;
}
}
/* =================================================================== */
// THIS CLASS IS LINE NUMBER SENSITIVE
class tc02t001Thread extends Thread {
public Wicket startingBarrier = new Wicket();
@ -139,3 +60,97 @@ class tc02t001Thread extends Thread {
}
}
}
/* =================================================================== */
public class tc02t001 extends DebugeeClass {
// run test from command line
public static void main(String argv[]) {
argv = nsk.share.jvmti.JVMTITest.commonInit(argv);
// JCK-compatible exit
System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
}
// run test from JCK-compatible environment
public static int run(String argv[], PrintStream out) {
return new tc02t001().runIt(argv, out);
}
/* =================================================================== */
// scaffold objects
ArgumentHandler argHandler = null;
Log log = null;
int status = Consts.TEST_PASSED;
static long timeout = 0;
private static volatile int lastEnterEventsCount;
private static native int enterEventsCount();
// tested thread
tc02t001Thread thread = null;
static void log (String msg) { System.out.println(msg); }
private void waitForContendedEnterEvent() {
try {
for (int j = 0; j < (timeout / 20); j++) {
Thread.sleep(20);
if (enterEventsCount() > lastEnterEventsCount) {
log("Got expected MonitorContendedEnter event\n");
break;
}
}
if (enterEventsCount() == lastEnterEventsCount) {
String msg = "Timeout in waiting for a MonitorContendedEnter event";
throw new RuntimeException(msg);
}
thread.M.wait(timeout);
} catch (InterruptedException e) {
throw new Failure(e);
}
}
// run debuggee
public int runIt(String argv[], PrintStream out) {
argHandler = new ArgumentHandler(argv);
log = new Log(out, argHandler);
timeout = argHandler.getWaitTime() * 60 * 1000;
log.display("Timeout = " + timeout + " msc.");
thread = new tc02t001Thread("Debuggee Thread");
synchronized (thread.M) {
thread.start();
thread.startingBarrier.waitFor();
status = checkStatus(status);
lastEnterEventsCount = enterEventsCount();
thread.waitingBarrier1.unlock();
log("Waiting for MonitorEnterEvent #1");
waitForContendedEnterEvent();
lastEnterEventsCount = enterEventsCount();
thread.waitingBarrier2.unlock();
log("Waiting for MonitorEnterEvent #2");
waitForContendedEnterEvent();
lastEnterEventsCount = enterEventsCount();
thread.waitingBarrier3.unlock();
log("Waiting for MonitorEnterEvent #3");
waitForContendedEnterEvent();
}
try {
thread.join(timeout);
} catch (InterruptedException e) {
throw new Failure(e);
}
log.display("Debugee finished");
status = checkStatus(status);
return status;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 2019, 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
@ -38,7 +38,7 @@ static jlong timeout = 0;
static jthread thread = NULL;
static jobject object_M = NULL;
/* line numbers of "synchronized (M)" clauses in java part of the test */
static jint lines[] = { 127, 132, 137 };
static jint lines[] = { 48, 53, 58 };
static volatile int enterEventsCount = 0;
static volatile int enteredEventsCount = 0;
@ -370,6 +370,11 @@ jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
return JNI_OK;
}
JNIEXPORT jint JNICALL
Java_nsk_jvmti_scenarios_contention_TC02_tc02t001_enterEventsCount(JNIEnv* jni, jclass klass) {
return enterEventsCount;
}
/* ========================================================================== */
}