test/jdk/java/lang/management/MemoryMXBean/MemoryTest.java
author pliden
Tue, 12 Jun 2018 17:40:28 +0200
changeset 50525 767cdb97f103
parent 50408 a5fc0fb1d31d
permissions -rw-r--r--
8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental) Reviewed-by: pliden, stefank, eosterlund, ehelin, sjohanss, rbackman, coleenp, ihse, jgeorge, lmesnik, rkennke Contributed-by: per.liden@oracle.com, stefan.karlsson@oracle.com, erik.osterlund@oracle.com, mikael.gerdin@oracle.com, kim.barrett@oracle.com, nils.eliasson@oracle.com, rickard.backman@oracle.com, rwestrel@redhat.com, coleen.phillimore@oracle.com, robbin.ehn@oracle.com, gerard.ziemski@oracle.com, hugh.wilkinson@intel.com, sandhya.viswanathan@intel.com, bill.npo.wheeler@intel.com, vinay.k.awasthi@intel.com, yasuenag@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
37890
f6cb5112c878 8156695: Fix @modules in tests in java/lang/management
shurailine
parents: 30376
diff changeset
     2
 * Copyright (c) 2003, 2016, 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 MemoryMXBean.getMemoryPools() and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          MemoryMXBean.getMemoryManager().
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents: 50408
diff changeset
    29
 * @requires vm.gc != "Z"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @author  Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
37890
f6cb5112c878 8156695: Fix @modules in tests in java/lang/management
shurailine
parents: 30376
diff changeset
    32
 * @modules jdk.management
50408
a5fc0fb1d31d 8204173: Lower the minimum number of heap memory pools in MemoryTest.java
stefank
parents: 47216
diff changeset
    33
 * @run main MemoryTest 2 3
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * NOTE: This expected result is hardcoded in this test and this test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *       will be affected if the heap memory layout is changed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *       the future implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class MemoryTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static boolean testFailed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final int HEAP = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int NONHEAP = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final int NUM_TYPES = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // WARNING: if the number of pools changes in the future,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // this test needs to be modified to handle different version of VMs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // Hotspot VM 1.5 expected to have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    //   heap memory pools     = 3 (Eden, Survivor spaces, Old gen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    //   non-heap memory pools = 2 (Perm gen, Code cache)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    //                           or 4 if Class Sharing is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // Number of memory managers = 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // They are: Copy/Scavenger + MSC + CodeCache manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // (or equivalent for other collectors)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // Number of GC memory managers = 2
13784
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
    63
26867
904e243eac08 8015774: Add support for multiple code heaps
thartmann
parents: 23010
diff changeset
    64
    // Hotspot VM 1.8+ after perm gen removal is expected to have between two
904e243eac08 8015774: Add support for multiple code heaps
thartmann
parents: 23010
diff changeset
    65
    // or five non-heap memory pools:
904e243eac08 8015774: Add support for multiple code heaps
thartmann
parents: 23010
diff changeset
    66
    // - Code cache (between one and three depending on the -XX:SegmentedCodeCache option)
18785
92a4cdaeca0f 8010734: NPG: The test MemoryTest.java needs to be updated to support metaspace
ehelin
parents: 14342
diff changeset
    67
    // - Metaspace
92a4cdaeca0f 8010734: NPG: The test MemoryTest.java needs to be updated to support metaspace
ehelin
parents: 14342
diff changeset
    68
    // - Compressed Class Space (if compressed class pointers are used)
50408
a5fc0fb1d31d 8204173: Lower the minimum number of heap memory pools in MemoryTest.java
stefank
parents: 47216
diff changeset
    69
a5fc0fb1d31d 8204173: Lower the minimum number of heap memory pools in MemoryTest.java
stefank
parents: 47216
diff changeset
    70
    private static int[] expectedMinNumPools = new int[2];
a5fc0fb1d31d 8204173: Lower the minimum number of heap memory pools in MemoryTest.java
stefank
parents: 47216
diff changeset
    71
    private static int[] expectedMaxNumPools = new int[2];
a5fc0fb1d31d 8204173: Lower the minimum number of heap memory pools in MemoryTest.java
stefank
parents: 47216
diff changeset
    72
    private static int expectedNumGCMgrs;
a5fc0fb1d31d 8204173: Lower the minimum number of heap memory pools in MemoryTest.java
stefank
parents: 47216
diff changeset
    73
    private static int expectedNumMgrs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static String[] types = { "heap", "non-heap" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static void main(String args[]) throws Exception {
50408
a5fc0fb1d31d 8204173: Lower the minimum number of heap memory pools in MemoryTest.java
stefank
parents: 47216
diff changeset
    77
        expectedNumGCMgrs = Integer.valueOf(args[0]);
18785
92a4cdaeca0f 8010734: NPG: The test MemoryTest.java needs to be updated to support metaspace
ehelin
parents: 14342
diff changeset
    78
        expectedNumMgrs = expectedNumGCMgrs + 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
50408
a5fc0fb1d31d 8204173: Lower the minimum number of heap memory pools in MemoryTest.java
stefank
parents: 47216
diff changeset
    80
        int expectedNumPools = Integer.valueOf(args[1]);
a5fc0fb1d31d 8204173: Lower the minimum number of heap memory pools in MemoryTest.java
stefank
parents: 47216
diff changeset
    81
        expectedMinNumPools[HEAP] = expectedNumPools;
a5fc0fb1d31d 8204173: Lower the minimum number of heap memory pools in MemoryTest.java
stefank
parents: 47216
diff changeset
    82
        expectedMaxNumPools[HEAP] = expectedNumPools;
a5fc0fb1d31d 8204173: Lower the minimum number of heap memory pools in MemoryTest.java
stefank
parents: 47216
diff changeset
    83
a5fc0fb1d31d 8204173: Lower the minimum number of heap memory pools in MemoryTest.java
stefank
parents: 47216
diff changeset
    84
        expectedMinNumPools[NONHEAP] = 2;
a5fc0fb1d31d 8204173: Lower the minimum number of heap memory pools in MemoryTest.java
stefank
parents: 47216
diff changeset
    85
        expectedMaxNumPools[NONHEAP] = 5;
a5fc0fb1d31d 8204173: Lower the minimum number of heap memory pools in MemoryTest.java
stefank
parents: 47216
diff changeset
    86
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        checkMemoryPools();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        checkMemoryManagers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (testFailed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new RuntimeException("TEST FAILED.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static void checkMemoryPools() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        List pools = ManagementFactory.getMemoryPoolMXBeans();
13784
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
    98
        boolean hasPerm = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        int[] numPools = new int[NUM_TYPES];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        for (ListIterator iter = pools.listIterator(); iter.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (pool.getType() == MemoryType.HEAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                numPools[HEAP]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (pool.getType() == MemoryType.NON_HEAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                numPools[NONHEAP]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
13784
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   109
            if (pool.getName().toLowerCase().contains("perm")) {
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   110
                hasPerm = true;
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   111
            }
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   112
        }
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   113
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   114
        if (hasPerm) {
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   115
            // If the VM has perm gen there will be between 2 and 4 non heap
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   116
            // pools (4 if class data sharing is used)
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   117
            expectedMinNumPools[NONHEAP] = 2;
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   118
            expectedMaxNumPools[NONHEAP] = 4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // Check the number of Memory pools
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        for (int i = 0; i < NUM_TYPES; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (numPools[i] < expectedMinNumPools[i] ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    numPools[i] > expectedMaxNumPools[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    "Number of " + types[i] + " pools = " + numPools[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    " but expected <= " + expectedMaxNumPools[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    " and >= " + expectedMinNumPools[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private static void checkMemoryManagers() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        List mgrs = ManagementFactory.getMemoryManagerMXBeans();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        int numGCMgr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // Check the number of Memory Managers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        for (ListIterator iter = mgrs.listIterator(); iter.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            MemoryManagerMXBean mgr = (MemoryManagerMXBean) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            String[] poolNames = mgr.getMemoryPoolNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (poolNames == null || poolNames.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    "Expected to have one or more pools for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    mgr.getName() + "manager.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (mgr instanceof GarbageCollectorMXBean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                numGCMgr++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                for (int i = 0; i < poolNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    checkPoolType(poolNames[i], MemoryType.NON_HEAP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (mgrs.size() != expectedNumMgrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                "Number of memory managers = " + mgrs.size() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                " but expected = " + expectedNumMgrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (numGCMgr != expectedNumGCMgrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                "Number of GC managers = " + numGCMgr + " but expected = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                expectedNumGCMgrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private static List pools = ManagementFactory.getMemoryPoolMXBeans();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private static void checkPoolType(String name, MemoryType type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        for (ListIterator iter = pools.listIterator(); iter.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (pool.getName().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                if (pool.getType() != type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        "Pool " + pool.getName() + " is of type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        pool.getType() + " but expected to be " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    return;
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
        throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            "Pool " + name + " is of type " + type +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            " not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
}