jdk/test/java/lang/management/MemoryMXBean/MemoryTest.java
author ykantser
Thu, 07 May 2015 09:11:49 +0200
changeset 30376 2ccf2cf7ea48
parent 26867 904e243eac08
child 37890 f6cb5112c878
permissions -rw-r--r--
8078896: Add @modules as needed to the jdk_svc tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 26867
diff changeset
     2
 * Copyright (c) 2003, 2015, 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().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author  Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 26867
diff changeset
    31
 * @modules java.management
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main MemoryTest 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * NOTE: This expected result is hardcoded in this test and this test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *       will be affected if the heap memory layout is changed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *       the future implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class MemoryTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static boolean testFailed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final int HEAP = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final int NONHEAP = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int NUM_TYPES = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // WARNING: if the number of pools changes in the future,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // this test needs to be modified to handle different version of VMs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // Hotspot VM 1.5 expected to have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    //   heap memory pools     = 3 (Eden, Survivor spaces, Old gen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    //   non-heap memory pools = 2 (Perm gen, Code cache)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    //                           or 4 if Class Sharing is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // Number of memory managers = 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // They are: Copy/Scavenger + MSC + CodeCache manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // (or equivalent for other collectors)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // Number of GC memory managers = 2
13784
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
    62
26867
904e243eac08 8015774: Add support for multiple code heaps
thartmann
parents: 23010
diff changeset
    63
    // 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
    64
    // or five non-heap memory pools:
904e243eac08 8015774: Add support for multiple code heaps
thartmann
parents: 23010
diff changeset
    65
    // - 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
    66
    // - Metaspace
92a4cdaeca0f 8010734: NPG: The test MemoryTest.java needs to be updated to support metaspace
ehelin
parents: 14342
diff changeset
    67
    // - Compressed Class Space (if compressed class pointers are used)
92a4cdaeca0f 8010734: NPG: The test MemoryTest.java needs to be updated to support metaspace
ehelin
parents: 14342
diff changeset
    68
    private static int[] expectedMinNumPools = {3, 2};
26867
904e243eac08 8015774: Add support for multiple code heaps
thartmann
parents: 23010
diff changeset
    69
    private static int[] expectedMaxNumPools = {3, 5};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static int expectedNumGCMgrs = 2;
18785
92a4cdaeca0f 8010734: NPG: The test MemoryTest.java needs to be updated to support metaspace
ehelin
parents: 14342
diff changeset
    71
    private static int expectedNumMgrs = expectedNumGCMgrs + 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static String[] types = { "heap", "non-heap" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        Integer value = new Integer(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        expectedNumGCMgrs = value.intValue();
18785
92a4cdaeca0f 8010734: NPG: The test MemoryTest.java needs to be updated to support metaspace
ehelin
parents: 14342
diff changeset
    77
        expectedNumMgrs = expectedNumGCMgrs + 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        checkMemoryPools();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        checkMemoryManagers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (testFailed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throw new RuntimeException("TEST FAILED.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static void checkMemoryPools() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        List pools = ManagementFactory.getMemoryPoolMXBeans();
13784
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
    90
        boolean hasPerm = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        int[] numPools = new int[NUM_TYPES];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        for (ListIterator iter = pools.listIterator(); iter.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (pool.getType() == MemoryType.HEAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                numPools[HEAP]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (pool.getType() == MemoryType.NON_HEAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                numPools[NONHEAP]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
13784
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   101
            if (pool.getName().toLowerCase().contains("perm")) {
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   102
                hasPerm = true;
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   103
            }
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   104
        }
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   105
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   106
        if (hasPerm) {
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   107
            // 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
   108
            // pools (4 if class data sharing is used)
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   109
            expectedMinNumPools[NONHEAP] = 2;
b362a531f1b6 7195557: NPG: Unexpected number of memory pools
mgerdin
parents: 5506
diff changeset
   110
            expectedMaxNumPools[NONHEAP] = 4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // Check the number of Memory pools
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        for (int i = 0; i < NUM_TYPES; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (numPools[i] < expectedMinNumPools[i] ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    numPools[i] > expectedMaxNumPools[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    "Number of " + types[i] + " pools = " + numPools[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    " but expected <= " + expectedMaxNumPools[i] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    " and >= " + expectedMinNumPools[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
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
    private static void checkMemoryManagers() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        List mgrs = ManagementFactory.getMemoryManagerMXBeans();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        int numGCMgr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        // Check the number of Memory Managers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        for (ListIterator iter = mgrs.listIterator(); iter.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            MemoryManagerMXBean mgr = (MemoryManagerMXBean) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            String[] poolNames = mgr.getMemoryPoolNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (poolNames == null || poolNames.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    "Expected to have one or more pools for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    mgr.getName() + "manager.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (mgr instanceof GarbageCollectorMXBean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                numGCMgr++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                for (int i = 0; i < poolNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    checkPoolType(poolNames[i], MemoryType.NON_HEAP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (mgrs.size() != expectedNumMgrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                "Number of memory managers = " + mgrs.size() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                " but expected = " + expectedNumMgrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (numGCMgr != expectedNumGCMgrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                "Number of GC managers = " + numGCMgr + " but expected = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                expectedNumGCMgrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private static List pools = ManagementFactory.getMemoryPoolMXBeans();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private static void checkPoolType(String name, MemoryType type)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        for (ListIterator iter = pools.listIterator(); iter.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (pool.getName().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                if (pool.getType() != type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        "Pool " + pool.getName() + " is of type " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                        pool.getType() + " but expected to be " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            "Pool " + name + " is of type " + type +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            " not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}