jdk/test/java/lang/management/MemoryMXBean/LowMemoryTest.java
author ykantser
Fri, 09 May 2014 15:24:57 +0200
changeset 24366 57c0a8b7a936
parent 23933 754ee146b623
child 25999 1e494b91a5a5
permissions -rw-r--r--
8034960: Serviceability tests using @library failing with java.lang.NoClassDefFoundError Reviewed-by: sla, alanb
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: 5808
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug     4530538
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) setUsageThreshold() and getUsageThreshold()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          2) test low memory detection on the old generation.
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
 *
23933
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
    33
 * @library /lib/testlibrary/
24366
57c0a8b7a936 8034960: Serviceability tests using @library failing with java.lang.NoClassDefFoundError
ykantser
parents: 23933
diff changeset
    34
 * @build jdk.testlibrary.* LowMemoryTest MemoryUtil RunUtil
23933
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
    35
 * @run main/timeout=600 LowMemoryTest
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.*;
22075
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
    40
import java.util.concurrent.Phaser;
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 LowMemoryTest {
22075
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
    45
    private static final MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
    46
    private static final List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
    47
    private static final Phaser phaser = new Phaser(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static MemoryPoolMXBean mpool = null;
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 NUM_TRIGGERS = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final int NUM_CHUNKS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static long chunkSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
23933
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
    55
    /**
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
    56
     * Run the test multiple times with different GC versions.
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
    57
     * First with default command line specified by the framework.
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
    58
     * Then with GC versions specified by the test.
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
    59
     */
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
    60
    public static void main(String a[]) throws Throwable {
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
    61
        final String main = "LowMemoryTest$TestMain";
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
    62
        RunUtil.runTestKeepGcOpts(main);
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
    63
        RunUtil.runTestClearGcOpts(main, "-XX:+UseSerialGC");
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
    64
        RunUtil.runTestClearGcOpts(main, "-XX:+UseParallelGC");
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
    65
        RunUtil.runTestClearGcOpts(main, "-XX:+UseG1GC");
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
    66
        RunUtil.runTestClearGcOpts(main, "-XX:+UseConcMarkSweepGC");
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
    67
    }
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
    68
22075
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
    69
    private static volatile boolean listenerInvoked = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static class SensorListener implements NotificationListener {
22075
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
    71
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        public void handleNotification(Notification notif, Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            String type = notif.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                type.equals(MemoryNotificationInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                MemoryNotificationInfo minfo = MemoryNotificationInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    from((CompositeData) notif.getUserData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                MemoryUtil.printMemoryNotificationInfo(minfo, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                listenerInvoked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static class TestListener implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        private int triggers = 0;
22075
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
    89
        private final long[] count = new long[NUM_TRIGGERS * 2];
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
    90
        private final long[] usedMemory = new long[NUM_TRIGGERS * 2];
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
    91
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        public void handleNotification(Notification notif, Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            MemoryNotificationInfo minfo = MemoryNotificationInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                from((CompositeData) notif.getUserData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            count[triggers] = minfo.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            usedMemory[triggers] = minfo.getUsage().getUsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            triggers++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        public void checkResult() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (triggers != NUM_TRIGGERS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                throw new RuntimeException("Unexpected number of triggers = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    triggers + " but expected to be " + NUM_TRIGGERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            for (int i = 0; i < triggers; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                if (count[i] != i+1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    throw new RuntimeException("Unexpected count of" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        " notification #" + i +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        " count = " + count[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        " but expected to be " + (i+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                if (usedMemory[i] < newThreshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    throw new RuntimeException("Used memory = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        usedMemory[i] + " is less than the threshold = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        newThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private static long newThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
23933
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   123
    private static class TestMain {
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   124
        public static void main(String args[]) throws Exception {
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   125
            if (args.length > 0 && args[0].equals("trace")) {
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   126
                trace = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
23933
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   129
            // Find the Old generation which supports low memory detection
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   130
            ListIterator iter = pools.listIterator();
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   131
            while (iter.hasNext()) {
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   132
                MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   133
                if (p.getType() == MemoryType.HEAP &&
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   134
                    p.isUsageThresholdSupported()) {
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   135
                    mpool = p;
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   136
                    if (trace) {
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   137
                        System.out.println("Selected memory pool for low memory " +
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   138
                            "detection.");
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   139
                        MemoryUtil.printMemoryPool(mpool);
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   140
                    }
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   141
                    break;
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   142
                }
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   143
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
23933
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   145
            TestListener listener = new TestListener();
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   146
            SensorListener l2 = new SensorListener();
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   147
            NotificationEmitter emitter = (NotificationEmitter) mm;
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   148
            emitter.addNotificationListener(listener, null, null);
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   149
            emitter.addNotificationListener(l2, null, null);
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   150
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   151
            Thread allocator = new AllocatorThread();
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   152
            Thread sweeper = new SweeperThread();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
23933
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   154
            // Now set threshold
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   155
            MemoryUsage mu = mpool.getUsage();
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   156
            chunkSize = (mu.getMax() - mu.getUsed()) / 20;
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   157
            newThreshold = mu.getUsed() + (chunkSize * NUM_CHUNKS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
23933
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   159
            System.out.println("Setting threshold for " + mpool.getName() +
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   160
                " from " + mpool.getUsageThreshold() + " to " + newThreshold +
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   161
                ".  Current used = " + mu.getUsed());
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   162
            mpool.setUsageThreshold(newThreshold);
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   163
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   164
            if (mpool.getUsageThreshold() != newThreshold) {
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   165
                throw new RuntimeException("TEST FAILED: " +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                "Threshold for Memory pool " + mpool.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                "is " + mpool.getUsageThreshold() + " but expected to be" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                newThreshold);
23933
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   169
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
22075
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   171
23933
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   172
            allocator.start();
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   173
            // Force Allocator start first
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   174
            phaser.arriveAndAwaitAdvance();
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   175
            sweeper.start();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
22075
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   177
23933
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   178
            try {
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   179
                allocator.join();
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   180
                // Wait until AllocatorThread's done
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   181
                phaser.arriveAndAwaitAdvance();
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   182
                sweeper.join();
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   183
            } catch (InterruptedException e) {
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   184
                System.out.println("Unexpected exception:" + e);
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   185
                testFailed = true;
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   186
            }
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   187
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   188
            listener.checkResult();
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   189
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   190
            if (testFailed)
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   191
                throw new RuntimeException("TEST FAILED.");
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   192
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   193
            System.out.println(RunUtil.successMessage);
754ee146b623 8030628: Update java/lang/management/MemoryMXBean tests to ignore GC setting by jtreg
mtobiass
parents: 22075
diff changeset
   194
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private static void goSleep(long ms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            Thread.sleep(ms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        } catch (InterruptedException e) {
22075
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   202
            System.out.println("Unexpected exception:" + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
22075
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   207
    private static final List<Object> objectPool = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    static class AllocatorThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        public void doTask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            int iterations = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            int numElements = (int) (chunkSize / 4); // minimal object size
22075
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   212
            while (!listenerInvoked || mpool.getUsage().getUsed() < mpool.getUsageThreshold()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                iterations++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    System.out.println("   Iteration " + iterations +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        ": before allocation " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        mpool.getUsage().getUsed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                Object[] o = new Object[numElements];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                if (iterations <= NUM_CHUNKS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    // only hold a reference to the first NUM_CHUNKS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    // allocated objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    objectPool.add(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    System.out.println("               " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                        "  after allocation " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                        mpool.getUsage().getUsed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                goSleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
22075
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   235
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            for (int i = 1; i <= NUM_TRIGGERS; i++) {
22075
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   238
                // Sync with SweeperThread's second phase.
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   239
                phaser.arriveAndAwaitAdvance();
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   240
                System.out.println("AllocatorThread is doing task " + i +
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   241
                    " phase " + phaser.getPhase());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                doTask();
22075
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   243
                // Sync with SweeperThread's first phase.
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   244
                phaser.arriveAndAwaitAdvance();
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   245
                System.out.println("AllocatorThread done task " + i +
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   246
                    " phase " + phaser.getPhase());
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   247
                if (testFailed) {
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   248
                    return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    static class SweeperThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        private void doTask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            for (; mpool.getUsage().getUsed() >=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                       mpool.getUsageThreshold();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                // clear all allocated objects and invoke GC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                objectPool.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                mm.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                goSleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
22075
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   264
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            for (int i = 1; i <= NUM_TRIGGERS; i++) {
22075
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   267
                // Sync with AllocatorThread's first phase.
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   268
                phaser.arriveAndAwaitAdvance();
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   269
                System.out.println("SweepThread is doing task " + i +
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   270
                    " phase " + phaser.getPhase());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                doTask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                listenerInvoked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
22075
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   275
                // Sync with AllocatorThread's second phase.
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   276
                phaser.arriveAndAwaitAdvance();
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   277
                System.out.println("SweepThread done task " + i +
4c204174a8d9 8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents: 7668
diff changeset
   278
                    " phase " + phaser.getPhase());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                if (testFailed) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
}