jdk/test/java/lang/management/MemoryMXBean/CollectionUsageThreshold.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7019 f2f6b61d34ac
child 13784 b362a531f1b6
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7019
diff changeset
     2
 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
    26
 * @bug     4959889 6992968
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic unit test of memory management testing:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          1) setCollectionUsageThreshold() and getCollectionUsageThreshold()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          2) test notification emitted for two different memory pools.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @author  Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @build CollectionUsageThreshold MemoryUtil
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @run main/timeout=300 CollectionUsageThreshold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
    37
import java.lang.Thread.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.*;
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
    40
import java.util.concurrent.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.openmbean.CompositeData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class CollectionUsageThreshold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    46
    private static List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    47
    private static List<MemoryManagerMXBean> managers = ManagementFactory.getMemoryManagerMXBeans();
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    48
    private static Map<String, PoolRecord> result = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static boolean trace = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static boolean testFailed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final int EXPECTED_NUM_POOLS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final int NUM_GCS = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final int THRESHOLD = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static Checker checker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static int numGCs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
    57
    // semaphore to signal the arrival of a low memory notification
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
    58
    private static Semaphore signals = new Semaphore(0);
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
    59
    // barrier for the main thread to wait until the checker thread
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
    60
    // finishes checking the low memory notification result
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
    61
    private static CyclicBarrier barrier = new CyclicBarrier(2);
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
    62
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static class PoolRecord {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        private MemoryPoolMXBean pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        private int listenerInvoked = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        private long notifCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        PoolRecord(MemoryPoolMXBean p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            this.pool = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        int getListenerInvokedCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            return listenerInvoked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        long getNotifCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            return notifCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        MemoryPoolMXBean getPool() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            return pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        void addNotification(MemoryNotificationInfo minfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            listenerInvoked++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            notifCount = minfo.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    static class SensorListener implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        private int numNotifs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        public void handleNotification(Notification notif, Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            String type = notif.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                type.equals(MemoryNotificationInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                MemoryNotificationInfo minfo = MemoryNotificationInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    from((CompositeData) notif.getUserData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                MemoryUtil.printMemoryNotificationInfo(minfo, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                PoolRecord pr = (PoolRecord) result.get(minfo.getPoolName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                if (pr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    throw new RuntimeException("Pool " + minfo.getPoolName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                        " is not selected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                if (type != MemoryNotificationInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        MEMORY_COLLECTION_THRESHOLD_EXCEEDED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    throw new RuntimeException("Pool " + minfo.getPoolName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        " got unexpected notification type: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                        type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                pr.addNotification(minfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                synchronized (this) {
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   109
                    System.out.println("notifying the checker thread to check result");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    numNotifs++;
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   111
                    signals.release();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private static long newThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (args.length > 0 && args[0].equals("trace")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            trace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            MemoryUtil.printMemoryPools(pools);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            MemoryUtil.printMemoryManagers(managers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // Find the Old generation which supports low memory detection
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   129
        for (MemoryPoolMXBean p : pools) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            MemoryUsage u = p.getUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (p.isUsageThresholdSupported() && p.isCollectionUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                PoolRecord pr = new PoolRecord(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                result.put(p.getName(), pr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                if (result.size() == EXPECTED_NUM_POOLS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (result.size() != EXPECTED_NUM_POOLS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            throw new RuntimeException("Unexpected number of selected pools");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   143
        try {
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   144
            // This test creates a checker thread responsible for checking
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   145
            // the low memory notifications.  It blocks until a permit
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   146
            // from the signals semaphore is available.
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   147
            checker = new Checker("Checker thread");
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   148
            checker.setDaemon(true);
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   149
            checker.start();
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   150
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   151
            for (PoolRecord pr : result.values()) {
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   152
                pr.getPool().setCollectionUsageThreshold(THRESHOLD);
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   153
                System.out.println("Collection usage threshold of " +
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   154
                    pr.getPool().getName() + " set to " + THRESHOLD);
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   155
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   157
            SensorListener listener = new SensorListener();
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   158
            NotificationEmitter emitter = (NotificationEmitter) mm;
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   159
            emitter.addNotificationListener(listener, null, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   161
            // The main thread invokes GC to trigger the VM to perform
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   162
            // low memory detection and then waits until the checker thread
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   163
            // finishes its work to check for a low-memory notification.
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   164
            //
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   165
            // At GC time, VM will issue low-memory notification and invoke
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   166
            // the listener which will release a permit to the signals semaphore.
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   167
            // When the checker thread acquires the permit and finishes
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   168
            // checking the low-memory notification, it will also call
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   169
            // barrier.await() to signal the main thread to resume its work.
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   170
            for (int i = 0; i < NUM_GCS; i++) {
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   171
                invokeGC();
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   172
                barrier.await();
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   173
            }
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   174
        } finally {
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   175
            // restore the default
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   176
            for (PoolRecord pr : result.values()) {
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   177
                pr.getPool().setCollectionUsageThreshold(0);
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   178
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (testFailed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new RuntimeException("TEST FAILED.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   188
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private static void invokeGC() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        System.out.println("Calling System.gc()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        numGCs++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        mm.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (trace) {
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   195
            for (PoolRecord pr : result.values()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                System.out.println("Usage after GC for: " + pr.getPool().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                MemoryUtil.printMemoryUsage(pr.getPool().getUsage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    static class Checker extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        private boolean checkerReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        private int waiters = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        private boolean readyToCheck = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        Checker(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            while (true) {
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   211
                try {
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   212
                    signals.acquire(EXPECTED_NUM_POOLS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    checkResult();
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   214
                } catch (InterruptedException e) {
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   215
                    throw new RuntimeException(e);
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   216
                } catch (BrokenBarrierException e) {
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   217
                    throw new RuntimeException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   221
        private void checkResult() throws InterruptedException, BrokenBarrierException {
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
   222
            for (PoolRecord pr : result.values()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                if (pr.getListenerInvokedCount() != numGCs) {
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   224
                    fail("Listeners invoked count = " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                         pr.getListenerInvokedCount() + " expected to be " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                         numGCs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                if (pr.getNotifCount() != numGCs) {
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   229
                    fail("Notif Count = " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                         pr.getNotifCount() + " expected to be " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                         numGCs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                long count = pr.getPool().getCollectionUsageThresholdCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                if (count != numGCs) {
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   236
                    fail("CollectionUsageThresholdCount = " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                         count + " expected to be " + numGCs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                if (!pr.getPool().isCollectionUsageThresholdExceeded()) {
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   240
                    fail("isCollectionUsageThresholdExceeded" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                         " expected to be true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   244
            // wait until the main thread is waiting for notification
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   245
            barrier.await();
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   246
            System.out.println("notifying main thread to continue - result checking finished");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
7019
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   249
        private void fail(String msg) {
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   250
            // reset the barrier to cause BrokenBarrierException to avoid hanging
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   251
            barrier.reset();
f2f6b61d34ac 6992968: test/java/lang/management/MemoryMXBean/CollectionUsageThresholdConcMarkSweepGC.sh should not hang
mchung
parents: 5808
diff changeset
   252
            throw new RuntimeException(msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
}