changeset 55948:a3e3f3caf284

8223736: jvmti/scenarios/contention/TC04/tc04t001/TestDescription.java fails due to wrong number of MonitorContendedEntered events Summary: Fix the synchronization issue in the test Reviewed-by: cjplummer, dcubed, amenkov
author sspitsyn
date Thu, 20 Jun 2019 23:12:12 -0700
parents ced62a6a7bbe
children 68ef70c9a921
files test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/contention/TC04/tc04t001.java test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/contention/TC04/tc04t001/tc04t001.cpp
diffstat 2 files changed, 33 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/contention/TC04/tc04t001.java	Thu Jun 20 18:47:44 2019 -0700
+++ b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/contention/TC04/tc04t001.java	Thu Jun 20 23:12:12 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
@@ -24,6 +24,7 @@
 package nsk.jvmti.scenarios.contention.TC04;
 
 import java.io.PrintStream;
+import java.util.concurrent.*;
 
 import nsk.share.*;
 import nsk.share.jvmti.*;
@@ -31,6 +32,7 @@
 public class tc04t001 extends DebugeeClass {
 
     final static int THREADS_LIMIT = 2;
+    final static CountDownLatch threadsDoneSignal = new CountDownLatch(THREADS_LIMIT);
 
     // run test from command line
     public static void main(String argv[]) {
@@ -68,8 +70,8 @@
         }
 
         try {
-            for (int i = 0; i < THREADS_LIMIT; i++) {
-                threads[i].join(timeout/THREADS_LIMIT);
+            if (!threadsDoneSignal.await(timeout, TimeUnit.MILLISECONDS)) {
+                throw new RuntimeException("Threads timeout");
             }
         } catch (InterruptedException e) {
             throw new Failure(e);
@@ -122,6 +124,8 @@
 */
 
     private int id;
+    private static volatile int lastEnterEventsCount;
+    private static native   int enterEventsCount();
 
     public tc04t001Thread(int i) {
         super("Debuggee Thread " + i);
@@ -131,11 +135,13 @@
     public synchronized void run() {
         for (int i = 0; i < INCREMENT_LIMIT; i++) {
             flicker.waitFor(id);
+            lastEnterEventsCount = enterEventsCount();
             increment(id);
             try {
                 wait(1);
             } catch (InterruptedException e) {}
         }
+        tc04t001.threadsDoneSignal.countDown();
     }
 
     static synchronized void increment(int i) {
@@ -144,10 +150,24 @@
 */
         flicker.unlock(i);
         int temp = value;
-        for (int j = 0; j < DELAY; j++) ;
-        try {
-            sleep(500);
-        } catch (InterruptedException e) {}
+
+        // Wait in a loop for a MonitorContendedEnter event.
+        // Timeout is: 20ms * DELAY.
+        for (int j = 0; j < DELAY; j++) {
+            try {
+                sleep(20);
+            } catch (InterruptedException e) {}
+
+            if (enterEventsCount() > lastEnterEventsCount) {
+                break; // Got an expected MonitorContendedEnter event
+            }
+        }
+        System.out.println("Thread-" + i + ": increment event: " + enterEventsCount());
+
+        if (enterEventsCount() == lastEnterEventsCount) {
+            String msg = "Timeout in waiting for a MonitorContendedEnter event";
+            throw new RuntimeException(msg);
+        }
         value = temp + 1;
     }
 }
--- a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/contention/TC04/tc04t001/tc04t001.cpp	Thu Jun 20 18:47:44 2019 -0700
+++ b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/contention/TC04/tc04t001/tc04t001.cpp	Thu Jun 20 23:12:12 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
@@ -344,6 +344,11 @@
     return JNI_OK;
 }
 
+JNIEXPORT jint JNICALL
+Java_nsk_jvmti_scenarios_contention_TC04_tc04t001Thread_enterEventsCount(JNIEnv* jni, jclass klass) {
+    return enterEventsCount;
+}
+
 /* ========================================================================== */
 
 }