8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
authormchung
Thu, 19 Dec 2013 13:43:30 -0800
changeset 22075 4c204174a8d9
parent 22074 e25a2837787e
child 22076 89a7ce7fea7d
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout Reviewed-by: mchung Contributed-by: Tristan Yan <tristan.yan@oracle.com>
jdk/test/java/lang/management/MemoryMXBean/LowMemoryTest.java
jdk/test/java/lang/management/MemoryMXBean/LowMemoryTestConcMarkSweepGC.sh
jdk/test/java/lang/management/MemoryMXBean/LowMemoryTestParallelGC.sh
jdk/test/java/lang/management/MemoryMXBean/LowMemoryTestSerialGC.sh
--- a/jdk/test/java/lang/management/MemoryMXBean/LowMemoryTest.java	Thu Dec 19 18:13:19 2013 +0000
+++ b/jdk/test/java/lang/management/MemoryMXBean/LowMemoryTest.java	Thu Dec 19 13:43:30 2013 -0800
@@ -32,17 +32,21 @@
  *
  * @build LowMemoryTest MemoryUtil
  * @run main/othervm/timeout=600 LowMemoryTest
+ * @run main/othervm/timeout=600 -XX:+UseConcMarkSweepGC LowMemoryTest
+ * @run main/othervm/timeout=600 -XX:+UseParallelGC LowMemoryTest
+ * @run main/othervm/timeout=600 -XX:+UseSerialGC LowMemoryTest
  */
 
 import java.lang.management.*;
 import java.util.*;
+import java.util.concurrent.Phaser;
 import javax.management.*;
 import javax.management.openmbean.CompositeData;
 
 public class LowMemoryTest {
-    private static MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
-    private static List pools = ManagementFactory.getMemoryPoolMXBeans();
-    private static List managers = ManagementFactory.getMemoryManagerMXBeans();
+    private static final MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
+    private static final List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
+    private static final Phaser phaser = new Phaser(2);
     private static MemoryPoolMXBean mpool = null;
     private static boolean trace = false;
     private static boolean testFailed = false;
@@ -50,8 +54,9 @@
     private static final int NUM_CHUNKS = 2;
     private static long chunkSize;
 
-    private static boolean listenerInvoked = false;
+    private static volatile boolean listenerInvoked = false;
     static class SensorListener implements NotificationListener {
+        @Override
         public void handleNotification(Notification notif, Object handback) {
             String type = notif.getType();
             if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) ||
@@ -69,8 +74,9 @@
 
     static class TestListener implements NotificationListener {
         private int triggers = 0;
-        private long[] count = new long[NUM_TRIGGERS * 2];
-        private long[] usedMemory = new long[NUM_TRIGGERS * 2];
+        private final long[] count = new long[NUM_TRIGGERS * 2];
+        private final long[] usedMemory = new long[NUM_TRIGGERS * 2];
+        @Override
         public void handleNotification(Notification notif, Object handback) {
             MemoryNotificationInfo minfo = MemoryNotificationInfo.
                 from((CompositeData) notif.getUserData());
@@ -148,15 +154,20 @@
                 newThreshold);
         }
 
+
         allocator.start();
+        // Force Allocator start first
+        phaser.arriveAndAwaitAdvance();
         sweeper.start();
 
+
         try {
             allocator.join();
+            // Wait until AllocatorThread's done
+            phaser.arriveAndAwaitAdvance();
             sweeper.join();
         } catch (InterruptedException e) {
-            e.printStackTrace();
-            System.out.println("Unexpected exception.");
+            System.out.println("Unexpected exception:" + e);
             testFailed = true;
         }
 
@@ -173,45 +184,17 @@
         try {
             Thread.sleep(ms);
         } catch (InterruptedException e) {
-            e.printStackTrace();
-            System.out.println("Unexpected exception.");
+            System.out.println("Unexpected exception:" + e);
             testFailed = true;
         }
     }
 
-    private static Object go = new Object();
-    private static boolean waiting = false; // No thread is waiting.
-
-    // Synchronizes two thread. If no thread is waiting then wait
-    // for notification from a different thread  and if it is
-    // is waiting then send notification.
-    // In this test case this method is used to synchronize sweeper
-    // thread and alocater thread to reach a particular point.
-    private static void wait_or_notify() {
-        synchronized (go) {
-            if (waiting == false) {
-                waiting = true;
-                System.out.println(" Waiting ");
-                try {
-                    go.wait();
-                } catch (InterruptedException e) {
-                    e.printStackTrace();
-                    testFailed = true;
-                }
-                waiting = false;
-            } else {
-                System.out.println(" Notify ");
-                go.notify();
-            }
-        }
-    }
-
-    private static List objectPool = new ArrayList();
+    private static final List<Object> objectPool = new ArrayList<>();
     static class AllocatorThread extends Thread {
         public void doTask() {
             int iterations = 0;
             int numElements = (int) (chunkSize / 4); // minimal object size
-            while (!listenerInvoked) {
+            while (!listenerInvoked || mpool.getUsage().getUsed() < mpool.getUsageThreshold()) {
                 iterations++;
                 if (trace) {
                     System.out.println("   Iteration " + iterations +
@@ -234,23 +217,25 @@
                 goSleep(100);
             }
         }
+        @Override
         public void run() {
             for (int i = 1; i <= NUM_TRIGGERS; i++) {
-                System.out.println("AllocatorThread is doing task " + i);
+                // Sync with SweeperThread's second phase.
+                phaser.arriveAndAwaitAdvance();
+                System.out.println("AllocatorThread is doing task " + i +
+                    " phase " + phaser.getPhase());
                 doTask();
-                synchronized (sweep) {
-                    sweep.notify();
+                // Sync with SweeperThread's first phase.
+                phaser.arriveAndAwaitAdvance();
+                System.out.println("AllocatorThread done task " + i +
+                    " phase " + phaser.getPhase());
+                if (testFailed) {
+                    return;
                 }
-                // System.out.print(" Allocater Thread ");
-                // If sweeper thread is waiting then send notify
-                // else wait for notification from sweeper thread.
-                wait_or_notify();
-                if (testFailed) return;
             }
         }
     }
 
-    private static Object sweep = new Object();
     static class SweeperThread extends Thread {
         private void doTask() {
             for (; mpool.getUsage().getUsed() >=
@@ -261,28 +246,21 @@
                 goSleep(100);
             }
         }
+        @Override
         public void run() {
             for (int i = 1; i <= NUM_TRIGGERS; i++) {
-                synchronized (sweep) {
-                    while (!listenerInvoked) {
-                        try {
-                            sweep.wait();
-                        } catch (InterruptedException e) {
-                            e.printStackTrace();
-                            System.out.println("Unexpected exception.");
-                            testFailed = true;
-                        }
-                    }
-                }
-                System.out.println("SweepThread is doing task " + i);
+                // Sync with AllocatorThread's first phase.
+                phaser.arriveAndAwaitAdvance();
+                System.out.println("SweepThread is doing task " + i +
+                    " phase " + phaser.getPhase());
                 doTask();
 
                 listenerInvoked = false;
 
-                // System.out.print(" Sweeper Thread ");
-                // If Allocater thread is waiting wait send notify
-                // else wait for notfication from allocater thread.
-                wait_or_notify();
+                // Sync with AllocatorThread's second phase.
+                phaser.arriveAndAwaitAdvance();
+                System.out.println("SweepThread done task " + i +
+                    " phase " + phaser.getPhase());
                 if (testFailed) return;
             }
         }
--- a/jdk/test/java/lang/management/MemoryMXBean/LowMemoryTestConcMarkSweepGC.sh	Thu Dec 19 18:13:19 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,52 +0,0 @@
-#
-# Copyright (c) 2003, 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.
-#
-
-#
-# @test
-# @bug     4530538
-# @summary Test LowMemoryTest with concurrent mark sweep GC
-# @author  Mandy Chung
-#
-# @run build LowMemoryTest
-# @run shell/timeout=600 LowMemoryTestConcMarkSweepGC.sh
-#
-
-#Set appropriate jdk
-
-if [ ! -z "${TESTJAVA}" ] ; then
-     jdk="$TESTJAVA"
-else
-     echo "--Error: TESTJAVA must be defined as the pathname of a jdk to test."
-     exit 1
-fi
-
-runOne()
-{ 
-   echo "runOne $@"
-   $TESTJAVA/bin/java ${TESTVMOPTS} -classpath $TESTCLASSES $@ || exit 2
-}
-
-# Test LowMemoryTest with concurrent collector
-runOne -XX:+UseConcMarkSweepGC LowMemoryTest 
-
-exit 0
--- a/jdk/test/java/lang/management/MemoryMXBean/LowMemoryTestParallelGC.sh	Thu Dec 19 18:13:19 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,52 +0,0 @@
-#
-# Copyright (c) 2003, 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.
-#
-
-#
-# @test
-# @bug     4530538
-# @summary Test LowMemoryTest with parallel GC
-# @author  Mandy Chung
-#
-# @run build LowMemoryTest
-# @run shell/timeout=600 LowMemoryTestParallelGC.sh
-#
-
-#Set appropriate jdk
-
-if [ ! -z "${TESTJAVA}" ] ; then
-     jdk="$TESTJAVA"
-else
-     echo "--Error: TESTJAVA must be defined as the pathname of a jdk to test."
-     exit 1
-fi
-
-runOne()
-{ 
-   echo "runOne $@"
-   $TESTJAVA/bin/java ${TESTVMOPTS} -classpath $TESTCLASSES $@ || exit 2
-}
-
-# Test LowMemoryTest with parallel scavenger collector
-runOne -XX:+UseParallelGC LowMemoryTest 
-
-exit 0
--- a/jdk/test/java/lang/management/MemoryMXBean/LowMemoryTestSerialGC.sh	Thu Dec 19 18:13:19 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,52 +0,0 @@
-#
-# Copyright (c) 2003, 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.
-#
-
-#
-# @test
-# @bug     4530538
-# @summary Test LowMemoryTest with Serial GC
-# @author  Mandy Chung
-#
-# @run build LowMemoryTest
-# @run shell/timeout=600 LowMemoryTestSerialGC.sh
-#
-
-#Set appropriate jdk
-
-if [ ! -z "${TESTJAVA}" ] ; then
-     jdk="$TESTJAVA"
-else
-     echo "--Error: TESTJAVA must be defined as the pathname of a jdk to test."
-     exit 1
-fi
-
-runOne()
-{ 
-   echo "runOne $@"
-   $TESTJAVA/bin/java ${TESTVMOPTS} -classpath $TESTCLASSES $@ || exit 2
-}
-
-# Test LowMemoryTest with serial collector
-runOne -XX:+UseSerialGC LowMemoryTest 
-
-exit 0