jdk/test/com/sun/management/HotSpotDiagnosticMXBean/SetVMOption.java
author alanb
Fri, 11 Jan 2013 20:19:55 +0000
changeset 15254 3997a6f357cb
parent 5506 202f599c92aa
child 18801 ff4643bb836d
permissions -rw-r--r--
8005978: shell tests need to use the $COMPILEJDK for javac, jar and other tools Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     2
 * Copyright (c) 2005, 2008, 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: 715
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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     6314913
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic Test for HotSpotDiagnosticMXBean.setVMOption()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          and getDiagnosticOptions().
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/othervm -XX:+PrintGCDetails SetVMOption
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    34
import java.lang.management.ManagementFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.management.HotSpotDiagnosticMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.management.VMOption;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.management.VMOption.Origin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.misc.Version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class SetVMOption {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static String PRINT_GC_DETAILS = "PrintGCDetails";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static String EXPECTED_VALUE = "true";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static String BAD_VALUE = "yes";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static String NEW_VALUE = "false";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static String MANAGEMENT_SERVER = "ManagementServer";
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    47
    private static HotSpotDiagnosticMXBean mbean;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static void main(String[] args) throws Exception {
401
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    50
        List<HotSpotDiagnosticMXBean> list =
ef01e0dccd63 6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents: 2
diff changeset
    51
            ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        // The following test is transitional only and should be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        // once build 52 is promoted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        int build = Version.jvmBuildNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (build > 0 && build < 52) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
             // JVM support is integrated in build 52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
             // this test is skipped if running with VM earlier than 52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
             return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        VMOption option = findPrintGCDetailsOption();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (!option.getValue().equalsIgnoreCase(EXPECTED_VALUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            throw new RuntimeException("Unexpected value: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                option.getValue() + " expected: " + EXPECTED_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (option.getOrigin() != Origin.VM_CREATION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            throw new RuntimeException("Unexpected origin: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                option.getOrigin() + " expected: VM_CREATION");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (!option.isWriteable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throw new RuntimeException("Expected " + PRINT_GC_DETAILS +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                " to be writeable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        // set VM option to a new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        mbean.setVMOption(PRINT_GC_DETAILS, NEW_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        option = findPrintGCDetailsOption();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (!option.getValue().equalsIgnoreCase(NEW_VALUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new RuntimeException("Unexpected value: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                option.getValue() + " expected: " + NEW_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (option.getOrigin() != Origin.MANAGEMENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throw new RuntimeException("Unexpected origin: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                option.getOrigin() + " expected: MANAGEMENT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        VMOption o = mbean.getVMOption(PRINT_GC_DETAILS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (!option.getValue().equals(o.getValue())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new RuntimeException("Unmatched value: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                option.getValue() + " expected: " + o.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (!option.getValue().equals(o.getValue())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new RuntimeException("Unmatched value: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                option.getValue() + " expected: " + o.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (option.getOrigin() != o.getOrigin()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new RuntimeException("Unmatched origin: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                option.getOrigin() + " expected: " + o.getOrigin());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (option.isWriteable() != o.isWriteable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new RuntimeException("Unmatched writeable: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                option.isWriteable() + " expected: " + o.isWriteable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // check if ManagementServer is not writeable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        List<VMOption> options = mbean.getDiagnosticOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        VMOption mgmtServerOption = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        for (VMOption o1 : options) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (o1.getName().equals(MANAGEMENT_SERVER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                 mgmtServerOption = o1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                 break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (mgmtServerOption != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new RuntimeException(MANAGEMENT_SERVER +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                " is not expected to be writeable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        mgmtServerOption = mbean.getVMOption(MANAGEMENT_SERVER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (mgmtServerOption == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new RuntimeException(MANAGEMENT_SERVER +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                " should exist.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (mgmtServerOption.getOrigin() != Origin.DEFAULT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new RuntimeException(MANAGEMENT_SERVER +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                " should have the default value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (mgmtServerOption.isWriteable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new RuntimeException(MANAGEMENT_SERVER +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                " is not expected to be writeable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public static VMOption findPrintGCDetailsOption() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        List<VMOption> options = mbean.getDiagnosticOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        VMOption gcDetails = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        for (VMOption o : options) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (o.getName().equals(PRINT_GC_DETAILS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                 gcDetails = o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                 break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (gcDetails == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            throw new RuntimeException("VM option " + PRINT_GC_DETAILS +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                " not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return gcDetails;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}