jdk/test/java/lang/management/MemoryMXBean/LowMemoryTest.java
author mchung
Fri, 18 Jun 2010 09:35:22 -0700
changeset 5808 3a1f603c5ca7
parent 5506 202f599c92aa
child 7668 d4a77089c587
permissions -rw-r--r--
6961894: TEST_BUG: jdk_lang tests fail in samevm mode Summary: Fixed jdk_lang tests to run in samevm mode or mark to run in othervm Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2004, 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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.openmbean.CompositeData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class LowMemoryTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static List pools = ManagementFactory.getMemoryPoolMXBeans();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static List managers = ManagementFactory.getMemoryManagerMXBeans();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static MemoryPoolMXBean mpool = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static boolean trace = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static boolean testFailed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int NUM_TRIGGERS = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final int NUM_CHUNKS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static long chunkSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static boolean listenerInvoked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static class SensorListener implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        public void handleNotification(Notification notif, Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            String type = notif.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                type.equals(MemoryNotificationInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                    MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                MemoryNotificationInfo minfo = MemoryNotificationInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                    from((CompositeData) notif.getUserData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                MemoryUtil.printMemoryNotificationInfo(minfo, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                listenerInvoked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static class TestListener implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        private int triggers = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        private long[] count = new long[NUM_TRIGGERS * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        private long[] usedMemory = new long[NUM_TRIGGERS * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        public void handleNotification(Notification notif, Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            MemoryNotificationInfo minfo = MemoryNotificationInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                from((CompositeData) notif.getUserData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            count[triggers] = minfo.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            usedMemory[triggers] = minfo.getUsage().getUsed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            triggers++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        public void checkResult() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (triggers != NUM_TRIGGERS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                throw new RuntimeException("Unexpected number of triggers = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    triggers + " but expected to be " + NUM_TRIGGERS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            for (int i = 0; i < triggers; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                if (count[i] != i+1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    throw new RuntimeException("Unexpected count of" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                        " notification #" + i +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                        " count = " + count[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        " but expected to be " + (i+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                if (usedMemory[i] < newThreshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    throw new RuntimeException("Used memory = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        usedMemory[i] + " is less than the threshold = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        newThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private static long newThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (args.length > 0 && args[0].equals("trace")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            trace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // Find the Old generation which supports low memory detection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        ListIterator iter = pools.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (p.getType() == MemoryType.HEAP &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    p.isUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                mpool = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    System.out.println("Selected memory pool for low memory " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        "detection.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    MemoryUtil.printMemoryPool(mpool);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        TestListener listener = new TestListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        SensorListener l2 = new SensorListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        NotificationEmitter emitter = (NotificationEmitter) mm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        emitter.addNotificationListener(listener, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        emitter.addNotificationListener(l2, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        Thread allocator = new AllocatorThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Thread sweeper = new SweeperThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // Now set threshold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        MemoryUsage mu = mpool.getUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        chunkSize = (mu.getMax() - mu.getUsed()) / 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        newThreshold = mu.getUsed() + (chunkSize * NUM_CHUNKS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        System.out.println("Setting threshold for " + mpool.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            " from " + mpool.getUsageThreshold() + " to " + newThreshold +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            ".  Current used = " + mu.getUsed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        mpool.setUsageThreshold(newThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (mpool.getUsageThreshold() != newThreshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                "Threshold for Memory pool " + mpool.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                "is " + mpool.getUsageThreshold() + " but expected to be" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                newThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        allocator.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        sweeper.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            allocator.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            sweeper.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        listener.checkResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (testFailed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw new RuntimeException("TEST FAILED.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private static void goSleep(long ms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            Thread.sleep(ms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private static Object go = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private static boolean waiting = false; // No thread is waiting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    // Synchronizes two thread. If no thread is waiting then wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    // for notification from a different thread  and if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    // is waiting then send notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    // In this test case this method is used to synchronize sweeper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    // thread and alocater thread to reach a particular point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private static void wait_or_notify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        synchronized (go) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (waiting == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                waiting = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                System.out.println(" Waiting ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    go.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                waiting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                System.out.println(" Notify ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                go.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private static List objectPool = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    static class AllocatorThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        public void doTask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            int iterations = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            int numElements = (int) (chunkSize / 4); // minimal object size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            while (!listenerInvoked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                iterations++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    System.out.println("   Iteration " + iterations +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        ": before allocation " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        mpool.getUsage().getUsed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                Object[] o = new Object[numElements];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                if (iterations <= NUM_CHUNKS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    // only hold a reference to the first NUM_CHUNKS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    // allocated objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    objectPool.add(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    System.out.println("               " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                        "  after allocation " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                        mpool.getUsage().getUsed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                goSleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            for (int i = 1; i <= NUM_TRIGGERS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                System.out.println("AllocatorThread is doing task " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                doTask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                synchronized (sweep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    sweep.notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                // System.out.print(" Allocater Thread ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                // If sweeper thread is waiting then send notify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                // else wait for notification from sweeper thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                wait_or_notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                if (testFailed) return;
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
    private static Object sweep = new Object();
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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            for (int i = 1; i <= NUM_TRIGGERS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                synchronized (sweep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    while (!listenerInvoked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                            sweep.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                            System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                System.out.println("SweepThread is doing task " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                doTask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                listenerInvoked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                // System.out.print(" Sweeper Thread ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                // If Allocater thread is waiting wait send notify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                // else wait for notfication from allocater thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                wait_or_notify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                if (testFailed) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
}