From a9dff9420ac60a0cc7c0901bd5b2b1c413d6083b Mon Sep 17 00:00:00 2001
From: Alex Menkov <amenkov@openjdk.org>
Date: Fri, 6 Nov 2020 21:57:43 +0000
Subject: [PATCH] 8254864:
 vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted001/TestDescription.java
 timed out

Reviewed-by: sspitsyn, cjplummer
---
 .../nsk/jvmti/ResourceExhausted/Helper.java   | 19 +++++++++++---
 .../jvmti/ResourceExhausted/resexhausted.cpp  | 14 +++++------
 .../ResourceExhausted/resexhausted001.java    | 25 +++++++++++++------
 .../resexhausted001/TestDescription.java      |  2 ++
 .../ResourceExhausted/resexhausted002.java    |  3 ++-
 .../ResourceExhausted/resexhausted003.java    |  4 ++-
 6 files changed, 46 insertions(+), 21 deletions(-)

diff --git a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/Helper.java b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/Helper.java
index 33d0b89b0ec..72b00cf9b40 100644
--- a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/Helper.java
+++ b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/Helper.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2020, 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
@@ -22,17 +22,28 @@
  */
 package nsk.jvmti.ResourceExhausted;
 
+import jtreg.SkippedException;
+
 public class Helper {
 
-    static native boolean gotExhaustedEvent();
+    static native int getExhaustedEventFlags();
     static native void resetExhaustedEvent();
 
-    static boolean checkResult(String eventName) {
-        if ( ! gotExhaustedEvent() ) {
+    static final int JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR = 1;
+    static final int JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP = 2;
+    static final int JVMTI_RESOURCE_EXHAUSTED_THREADS = 4;
+
+    static boolean checkResult(int expectedFlag, String eventName) {
+        int got = getExhaustedEventFlags();
+        if (got == 0) {
             System.err.println("Failure: Expected ResourceExhausted event after " + eventName + " did not occur");
             return false;
         }
 
+        if ((got & expectedFlag) == 0) {
+            System.err.println("Warning: did not get expected flag bit (expected: "+ expectedFlag + ", got: " + got + ")");
+            throw new SkippedException("Test did not get expected flag value");
+        }
         System.out.println("Got expected ResourceExhausted event");
         return true;
     }
diff --git a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted.cpp b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted.cpp
index c6e795837e6..b3c6304ee0c 100644
--- a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted.cpp
+++ b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2020, 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
@@ -33,7 +33,7 @@
 extern "C" {
 
 static jvmtiEnv* gJvmti = NULL;
-static volatile jboolean gGotEvent = JNI_FALSE;
+static volatile jint gEventFlags = 0;
 
 void JNICALL
 resourceExhausted(jvmtiEnv *jvmti_env,
@@ -46,19 +46,19 @@ resourceExhausted(jvmtiEnv *jvmti_env,
     if (flags & JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR) NSK_DISPLAY0("Agent:    JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR\n");
     if (flags & JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP) NSK_DISPLAY0("Agent:    JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP\n");
     if (flags & JVMTI_RESOURCE_EXHAUSTED_THREADS)   NSK_DISPLAY0("Agent:    JVMTI_RESOURCE_EXHAUSTED_THREADS\n");
-    gGotEvent = JNI_TRUE;
+    gEventFlags = flags;
 }
 
-JNIEXPORT jboolean JNICALL
-Java_nsk_jvmti_ResourceExhausted_Helper_gotExhaustedEvent(JNIEnv* env, jclass cls)
+JNIEXPORT jint JNICALL
+Java_nsk_jvmti_ResourceExhausted_Helper_getExhaustedEventFlags(JNIEnv* env, jclass cls)
 {
-    return gGotEvent;
+    return gEventFlags;
 }
 
 JNIEXPORT void JNICALL
 Java_nsk_jvmti_ResourceExhausted_Helper_resetExhaustedEvent(JNIEnv* env, jclass cls)
 {
-    gGotEvent = JNI_FALSE;
+    gEventFlags = 0;
 }
 
 #ifdef STATIC_BUILD
diff --git a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted001.java b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted001.java
index ac497dfcb0e..b147757f9e0 100644
--- a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted001.java
+++ b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted001.java
@@ -25,6 +25,7 @@ package nsk.jvmti.ResourceExhausted;
 import java.io.PrintStream;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import jdk.test.lib.Platform;
 import nsk.share.Consts;
 import nsk.share.test.Stresser;
 import jtreg.SkippedException;
@@ -42,6 +43,11 @@ public class resexhausted001 {
 
     public static int run(String args[], PrintStream out) {
 
+        // Check platform here (instead of @requires) as this test is also called from resexhausted004
+        if (Platform.isWindows()) {
+            throw new SkippedException("Cannot get JVMTI_RESOURCE_EXHAUSTED_THREADS on Windows");
+        }
+
         Stresser stress = new Stresser(args);
 
         int count = 0;
@@ -56,14 +62,15 @@ public class resexhausted001 {
                 makeThread();
             }
 
-            System.out.println("Can't reproduce OOME due to a limit on iterations/execution time. Test was useless.");
+            System.out.println("Can't reproduce OOME due to a limit on iterations/execution time. Test was useless."
+                    + " threadCount=" + threadCount.get());
             throw new SkippedException("Test did not get an OutOfMemory error");
 
         } catch (OutOfMemoryError e) {
             count = threadCount.get();
         } finally {
-            threadsDone = true;
             synchronized (hanger) {
+                threadsDone = true;
                 hanger.notifyAll();
             }
             stress.finish();
@@ -74,7 +81,8 @@ public class resexhausted001 {
         }
 
         System.gc();
-        if (!Helper.checkResult("creating " + count + " threads")) {
+        System.out.println("got OOME with threadCount=" + count);
+        if (!Helper.checkResult(Helper.JVMTI_RESOURCE_EXHAUSTED_THREADS, "creating " + count + " threads")) {
             return Consts.TEST_FAILED;
         }
 
@@ -85,16 +93,17 @@ public class resexhausted001 {
         final Thread thr = new Thread(new Runnable() {
             public void run() {
                 threadCount.getAndIncrement();
-                while (!threadsDone) {
-                    try {
-                        synchronized (hanger) {
+                synchronized (hanger) {
+                    while (!threadsDone) {
+                        try {
                             hanger.wait();
-                        }
-                    } catch (InterruptedException ignored) {}
+                        } catch (InterruptedException ignored) {}
+                    }
                 }
                 threadCount.getAndDecrement();
             }
         }, "fleece");
+        thr.setDaemon(true);
         thr.start();
         return thr;
     }
diff --git a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted001/TestDescription.java b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted001/TestDescription.java
index 57f29ae5173..c86d34c9b2c 100644
--- a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted001/TestDescription.java
+++ b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted001/TestDescription.java
@@ -40,6 +40,8 @@
  * @run main/othervm/native/timeout=240
  *      -agentlib:resexhausted=-waittime=5
  *      -XX:-UseGCOverheadLimit
+ *      -Xms16m
+ *      -Xmx16m
  *      nsk.jvmti.ResourceExhausted.resexhausted001
  *      -stressTime 220
  */
diff --git a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted002.java b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted002.java
index 6871640833d..46832df7dc3 100644
--- a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted002.java
+++ b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted002.java
@@ -64,8 +64,9 @@ public class resexhausted002 {
         }
 
         System.gc();
-        if ( ! Helper.checkResult("creating " + count + " objects") )
+        if (!Helper.checkResult(Helper.JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP, "creating " + count + " objects")) {
             return Consts.TEST_FAILED;
+        }
 
         return Consts.TEST_PASSED;
     }
diff --git a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted003.java b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted003.java
index 9b5e28e6c8e..f336c1bf3d5 100644
--- a/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted003.java
+++ b/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted003.java
@@ -125,8 +125,10 @@ public class resexhausted003 {
         }
 
         System.gc();
-        if ( ! Helper.checkResult("loading " + count + " classes of " + bloatBytes.length + " bytes") )
+        if (!Helper.checkResult(Helper.JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR,
+                                "loading " + count + " classes of " + bloatBytes.length + " bytes")) {
             return Consts.TEST_FAILED;
+        }
 
         return Consts.TEST_PASSED;
     }