8224555: vmTestbase/nsk/jvmti/scenarios/contention/TC02/tc02t001/TestDescription.java failed
authorsspitsyn
Fri, 21 Jun 2019 18:20:49 -0700
changeset 55481 e17c9a93b505
parent 55480 f1e5ddb814b7
child 55482 4d5eabe8d341
8224555: vmTestbase/nsk/jvmti/scenarios/contention/TC02/tc02t001/TestDescription.java failed Summary: Improve synchronization in the test Reviewed-by: dcubed, amenkov
test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/contention/TC02/tc02t001.java
test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/contention/TC02/tc02t001/tc02t001.cpp
--- a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/contention/TC02/tc02t001.java	Fri Jun 21 16:20:01 2019 -0700
+++ b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/contention/TC02/tc02t001.java	Fri Jun 21 18:20:49 2019 -0700
@@ -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 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 @@
         }
     }
 }
+
+/* =================================================================== */
+
+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;
+    }
+}
--- a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/contention/TC02/tc02t001/tc02t001.cpp	Fri Jun 21 16:20:01 2019 -0700
+++ b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/contention/TC02/tc02t001/tc02t001.cpp	Fri Jun 21 18:20:49 2019 -0700
@@ -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 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 @@
     return JNI_OK;
 }
 
+JNIEXPORT jint JNICALL
+Java_nsk_jvmti_scenarios_contention_TC02_tc02t001_enterEventsCount(JNIEnv* jni, jclass klass) {
+    return enterEventsCount;
+}
+
 /* ========================================================================== */
 
 }