test/jdk/java/lang/management/GarbageCollectorMXBean/GcInfoCompositeType.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 40684 jdk/test/java/lang/management/GarbageCollectorMXBean/GcInfoCompositeType.java@2e37c119dc2a
child 59053 ba6c248cae19
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
40684
2e37c119dc2a 8164982: Fix legal notices in java/lang, java/net, java/util tests.
shurailine
parents: 37890
diff changeset
     2
 * Copyright (c) 2006, 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     6396794
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check that LastGcInfo contents are reasonable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author  Eamonn McManus
37890
f6cb5112c878 8156695: Fix @modules in tests in java/lang/management
shurailine
parents: 30376
diff changeset
    29
 * @modules jdk.management
21944
924b78f629be 6987597: ManagementFactory.getGarbageCollectorMXBeans() returns empty list with CMS
jbachorik
parents: 5506
diff changeset
    30
 * @run     main/othervm -XX:-ExplicitGCInvokesConcurrent GcInfoCompositeType
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
21944
924b78f629be 6987597: ManagementFactory.getGarbageCollectorMXBeans() returns empty list with CMS
jbachorik
parents: 5506
diff changeset
    32
// Passing "-XX:-ExplicitGCInvokesConcurrent" to force System.gc()
924b78f629be 6987597: ManagementFactory.getGarbageCollectorMXBeans() returns empty list with CMS
jbachorik
parents: 5506
diff changeset
    33
// run on foreground when CMS is used and prevent situations when "GcInfo"
924b78f629be 6987597: ManagementFactory.getGarbageCollectorMXBeans() returns empty list with CMS
jbachorik
parents: 5506
diff changeset
    34
// is missing even though System.gc() was successfuly processed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.openmbean.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import com.sun.management.GcInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class GcInfoCompositeType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static int tested = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        final ObjectName gcMXBeanPattern =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                new ObjectName("java.lang:type=GarbageCollector,*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        Set<ObjectName> names =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                mbs.queryNames(gcMXBeanPattern, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (names.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            throw new Exception("Test incorrect: no GC MXBeans");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        for (ObjectName n : names)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            tested += test(mbs, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (tested == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            throw new Exception("No MXBeans were tested");
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 int test(MBeanServer mbs, ObjectName n) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        System.out.println("Testing " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        MBeanInfo mbi = mbs.getMBeanInfo(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        MBeanAttributeInfo lastGcAI = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        for (MBeanAttributeInfo mbai : mbi.getAttributes()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            if (mbai.getName().equals("LastGcInfo")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                lastGcAI = mbai;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (lastGcAI == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new Exception("No LastGcInfo attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        CompositeType declaredType =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                (CompositeType) lastGcAI.getDescriptor().getFieldValue("openType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        checkType(declaredType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        CompositeData cd =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                (CompositeData) mbs.getAttribute(n, "LastGcInfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (cd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            System.out.println("Value of attribute null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            return checkType(cd.getCompositeType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static int checkType(CompositeType ct) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Method[] methods = GcInfo.class.getMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        Set<String> getters = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        for (Method m : methods) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (m.getName().startsWith("get") && m.getParameterTypes().length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                getters.add(m.getName().substring(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        Set<String> items = new HashSet<String>(ct.keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        System.out.println("Items at start: " + items);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // Now check that all the getters have a corresponding item in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // CompositeType, except the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // getClass() inherited from Object should not be an item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // getCompositeType() inherited from CompositeData is not useful so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // our hack removes it too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // Also track which items had corresponding getters, to make sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // there is at least one item which does not (GcThreadCount or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // another gc-type-specific item).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        final String[] surplus = {"Class", "CompositeType"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        for (String key : ct.keySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (getters.remove(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                items.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (!getters.equals(new HashSet<String>(Arrays.asList(surplus)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new Exception("Wrong getters: " + getters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (items.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            System.out.println("No type-specific items");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            System.out.println("Type-specific items: " + items);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
}