jdk/test/java/lang/management/MemoryPoolMXBean/ThresholdTest.java
author jbachorik
Wed, 30 Oct 2013 17:54:48 +0100
changeset 21413 d5d767325325
parent 7668 d4a77089c587
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
8020467: Inconsistency between usage.getUsed() and isUsageThresholdExceeded() with CMS Old Gen pool Reviewed-by: mchung, brutisso
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
21413
d5d767325325 8020467: Inconsistency between usage.getUsed() and isUsageThresholdExceeded() with CMS Old Gen pool
jbachorik
parents: 7668
diff changeset
     2
 * Copyright (c) 2007, 2013, 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
 *
21413
d5d767325325 8020467: Inconsistency between usage.getUsed() and isUsageThresholdExceeded() with CMS Old Gen pool
jbachorik
parents: 7668
diff changeset
    31
 * @run main/othervm ThresholdTest
2
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 {
21413
d5d767325325 8020467: Inconsistency between usage.getUsed() and isUsageThresholdExceeded() with CMS Old Gen pool
jbachorik
parents: 7668
diff changeset
    39
        long[] bigObject = new long[1000000];
d5d767325325 8020467: Inconsistency between usage.getUsed() and isUsageThresholdExceeded() with CMS Old Gen pool
jbachorik
parents: 7668
diff changeset
    40
d5d767325325 8020467: Inconsistency between usage.getUsed() and isUsageThresholdExceeded() with CMS Old Gen pool
jbachorik
parents: 7668
diff changeset
    41
        System.gc(); // force an initial full-gc
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    43
        try {
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    44
            for (MemoryPoolMXBean p : pools) {
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    45
                // verify if isUsageThresholdExceeded() returns correct value
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    46
                checkUsageThreshold(p);
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    47
                // verify if isCollectionUsageThresholdExceeded() returns correct value
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    48
                checkCollectionUsageThreshold(p);
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    49
            }
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    50
        } finally {
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    51
            // restore the default
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    52
            for (MemoryPoolMXBean p : pools) {
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    53
                if (p.isUsageThresholdSupported()) {
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    54
                    p.setUsageThreshold(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
                if (p.isCollectionUsageThresholdSupported()) {
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    57
                    p.setCollectionUsageThreshold(0);
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    58
                }
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    59
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static void checkUsageThreshold(MemoryPoolMXBean p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (!p.isUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        long threshold = p.getUsageThreshold();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (threshold != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            // Expect the default threshold is zero (disabled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                " has non-zero threshold (" + threshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // isUsageThresholdExceeded() should return false if threshold == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (p.isUsageThresholdExceeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                " isUsageThresholdExceeded() returned true" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                " but threshold = 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        p.setUsageThreshold(1);
21413
d5d767325325 8020467: Inconsistency between usage.getUsed() and isUsageThresholdExceeded() with CMS Old Gen pool
jbachorik
parents: 7668
diff changeset
    88
        // force a full gc to minimize the likelihood of running GC
d5d767325325 8020467: Inconsistency between usage.getUsed() and isUsageThresholdExceeded() with CMS Old Gen pool
jbachorik
parents: 7668
diff changeset
    89
        // between getting the usage and checking the threshold
d5d767325325 8020467: Inconsistency between usage.getUsed() and isUsageThresholdExceeded() with CMS Old Gen pool
jbachorik
parents: 7668
diff changeset
    90
        System.gc();
d5d767325325 8020467: Inconsistency between usage.getUsed() and isUsageThresholdExceeded() with CMS Old Gen pool
jbachorik
parents: 7668
diff changeset
    91
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        MemoryUsage u = p.getUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (u.getUsed() >= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (!p.isUsageThresholdExceeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    " isUsageThresholdExceeded() returned false but " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    " threshold(" + p.getUsageThreshold() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    ") <= used(" + u.getUsed() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (p.isUsageThresholdExceeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    " isUsageThresholdExceeded() returned true but" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    " threshold(" + p.getUsageThreshold() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    ") > used(" + u.getUsed() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        // disable low memory detection and isUsageThresholdExceeded()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // should return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        p.setUsageThreshold(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (p.isUsageThresholdExceeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                " isUsageThresholdExceeded() returned true but threshold = 0");
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 void checkCollectionUsageThreshold(MemoryPoolMXBean p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (!p.isCollectionUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        long threshold = p.getCollectionUsageThreshold();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (threshold != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            // Expect the default threshold is zero (disabled)
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
                " has non-zero threshold (" + threshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        // isCollectionUsageThresholdExceeded() should return false if threshold == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (p.isCollectionUsageThresholdExceeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                " isCollectionUsageThresholdExceeded() returned true" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                " but threshold = 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        p.setCollectionUsageThreshold(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        MemoryUsage u = p.getCollectionUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (u == null) {
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 true but" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    " getCollectionUsage() return null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } else if (u.getUsed() >= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (!p.isCollectionUsageThresholdExceeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    " isCollectionUsageThresholdExceeded() returned false but " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    " threshold(" + p.getCollectionUsageThreshold() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    ") < used(" + u.getUsed() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if (p.isCollectionUsageThresholdExceeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    " isCollectionUsageThresholdExceeded() returned true but" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    " threshold(" + p.getCollectionUsageThreshold() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    ") > used(" + u.getUsed() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // disable low memory detection and isCollectionUsageThresholdExceeded()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // should return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        p.setCollectionUsageThreshold(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (p.isCollectionUsageThresholdExceeded()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                "Pool " + p.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                " isCollectionUsageThresholdExceeded() returned true but threshold = 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}