jdk/test/java/lang/management/MemoryPoolMXBean/ThresholdTest.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5808 3a1f603c5ca7
child 21413 d5d767325325
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5808
diff changeset
     2
 * Copyright (c) 2007, 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     6546089
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic unit test of MemoryPoolMXBean.isUsageThresholdExceeded() and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          MemoryPoolMXBean.isCollectionThresholdExceeded().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author  Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main ThresholdTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class ThresholdTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    40
        try {
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    41
            for (MemoryPoolMXBean p : pools) {
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    42
                // verify if isUsageThresholdExceeded() returns correct value
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    43
                checkUsageThreshold(p);
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    44
                // verify if isCollectionUsageThresholdExceeded() returns correct value
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    45
                checkCollectionUsageThreshold(p);
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    46
            }
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    47
        } finally {
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    48
            // restore the default
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    49
            for (MemoryPoolMXBean p : pools) {
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    50
                if (p.isUsageThresholdSupported()) {
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    51
                    p.setUsageThreshold(0);
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    52
                }
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    53
                if (p.isCollectionUsageThresholdSupported()) {
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    54
                    p.setCollectionUsageThreshold(0);
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    55
                }
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    56
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static void checkUsageThreshold(MemoryPoolMXBean p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (!p.isUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        long threshold = p.getUsageThreshold();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (threshold != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            // Expect the default threshold is zero (disabled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                " has non-zero threshold (" + threshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // isUsageThresholdExceeded() should return false if threshold == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (p.isUsageThresholdExceeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                " isUsageThresholdExceeded() returned true" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                " but threshold = 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        p.setUsageThreshold(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        MemoryUsage u = p.getUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (u.getUsed() >= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (!p.isUsageThresholdExceeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    " isUsageThresholdExceeded() returned false but " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    " threshold(" + p.getUsageThreshold() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    ") <= used(" + u.getUsed() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (p.isUsageThresholdExceeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    " isUsageThresholdExceeded() returned true but" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    " threshold(" + p.getUsageThreshold() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    ") > used(" + u.getUsed() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // disable low memory detection and isUsageThresholdExceeded()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        // should return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        p.setUsageThreshold(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (p.isUsageThresholdExceeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                " isUsageThresholdExceeded() returned true but threshold = 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static void checkCollectionUsageThreshold(MemoryPoolMXBean p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (!p.isCollectionUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        long threshold = p.getCollectionUsageThreshold();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (threshold != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            // Expect the default threshold is zero (disabled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                " has non-zero threshold (" + threshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // isCollectionUsageThresholdExceeded() should return false if threshold == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (p.isCollectionUsageThresholdExceeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                " isCollectionUsageThresholdExceeded() returned true" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                " but threshold = 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        p.setCollectionUsageThreshold(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        MemoryUsage u = p.getCollectionUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (u == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            if (p.isCollectionUsageThresholdExceeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    " isCollectionUsageThresholdExceeded() returned true but" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    " getCollectionUsage() return null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        } else if (u.getUsed() >= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (!p.isCollectionUsageThresholdExceeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    " isCollectionUsageThresholdExceeded() returned false but " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    " threshold(" + p.getCollectionUsageThreshold() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    ") < used(" + u.getUsed() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (p.isCollectionUsageThresholdExceeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    " isCollectionUsageThresholdExceeded() returned true but" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    " threshold(" + p.getCollectionUsageThreshold() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    ") > used(" + u.getUsed() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        // disable low memory detection and isCollectionUsageThresholdExceeded()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // should return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        p.setCollectionUsageThreshold(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (p.isCollectionUsageThresholdExceeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                " isCollectionUsageThresholdExceeded() returned true but threshold = 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
}