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