Path: blob/master/test/jdk/com/sun/management/HotSpotDiagnosticMXBean/SetVMOption.java
41153 views
/*1* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 631491326* @summary Basic Test for HotSpotDiagnosticMXBean.setVMOption()27* and getDiagnosticOptions().28* @author Mandy Chung29* @author Jaroslav Bachorik30*31* @library /test/lib32* @run main/othervm -XX:+HeapDumpOnOutOfMemoryError SetVMOption33*/3435import java.lang.management.ManagementFactory;36import java.util.*;37import com.sun.management.HotSpotDiagnosticMXBean;38import com.sun.management.VMOption;39import com.sun.management.VMOption.Origin;40import jdk.test.lib.Platform;4142public class SetVMOption {43private static final String HEAP_DUMP_ON_OOM = "HeapDumpOnOutOfMemoryError";44private static final String EXPECTED_VALUE = "true";45private static final String BAD_VALUE = "yes";46private static final String NEW_VALUE = "false";47private static final String MANAGEMENT_SERVER = "ManagementServer";48private static HotSpotDiagnosticMXBean mbean;4950public static void main(String[] args) throws Exception {51mbean =52ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);5354VMOption option = findHeapDumpOnOomOption();55if (!option.getValue().equalsIgnoreCase(EXPECTED_VALUE)) {56throw new RuntimeException("Unexpected value: " +57option.getValue() + " expected: " + EXPECTED_VALUE);58}59if (option.getOrigin() != Origin.VM_CREATION) {60throw new RuntimeException("Unexpected origin: " +61option.getOrigin() + " expected: VM_CREATION");62}63if (!option.isWriteable()) {64throw new RuntimeException("Expected " + HEAP_DUMP_ON_OOM +65" to be writeable");66}6768// set VM option to a new value69mbean.setVMOption(HEAP_DUMP_ON_OOM, NEW_VALUE);7071option = findHeapDumpOnOomOption();72if (!option.getValue().equalsIgnoreCase(NEW_VALUE)) {73throw new RuntimeException("Unexpected value: " +74option.getValue() + " expected: " + NEW_VALUE);75}76if (option.getOrigin() != Origin.MANAGEMENT) {77throw new RuntimeException("Unexpected origin: " +78option.getOrigin() + " expected: MANAGEMENT");79}80VMOption o = mbean.getVMOption(HEAP_DUMP_ON_OOM);81if (!option.getValue().equals(o.getValue())) {82throw new RuntimeException("Unmatched value: " +83option.getValue() + " expected: " + o.getValue());84}85if (!option.getValue().equals(o.getValue())) {86throw new RuntimeException("Unmatched value: " +87option.getValue() + " expected: " + o.getValue());88}89if (option.getOrigin() != o.getOrigin()) {90throw new RuntimeException("Unmatched origin: " +91option.getOrigin() + " expected: " + o.getOrigin());92}93if (option.isWriteable() != o.isWriteable()) {94throw new RuntimeException("Unmatched writeable: " +95option.isWriteable() + " expected: " + o.isWriteable());96}979899// Today we don't have any manageable flags of the string type in the product build,100// so we can only test DummyManageableStringFlag in the debug build.101if (Platform.isDebugBuild()) {102String optionName = "DummyManageableStringFlag";103String toValue = "DummyManageableStringFlag_Is_Set_To_Hello";104105mbean.setVMOption(optionName, toValue);106107VMOption stringOption = findOption(optionName);108Object newValue = stringOption.getValue();109if (!toValue.equals(newValue)) {110throw new RuntimeException("Unmatched value: " +111newValue + " expected: " + toValue);112}113}114115// check if ManagementServer is not writeable116List<VMOption> options = mbean.getDiagnosticOptions();117VMOption mgmtServerOption = null;118for (VMOption o1 : options) {119if (o1.getName().equals(MANAGEMENT_SERVER)) {120mgmtServerOption = o1;121break;122}123}124if (mgmtServerOption != null) {125throw new RuntimeException(MANAGEMENT_SERVER +126" is not expected to be writeable");127}128mgmtServerOption = mbean.getVMOption(MANAGEMENT_SERVER);129if (mgmtServerOption == null) {130throw new RuntimeException(MANAGEMENT_SERVER +131" should exist.");132}133if (mgmtServerOption.getOrigin() != Origin.DEFAULT) {134throw new RuntimeException(MANAGEMENT_SERVER +135" should have the default value.");136}137if (mgmtServerOption.isWriteable()) {138throw new RuntimeException(MANAGEMENT_SERVER +139" is not expected to be writeable");140}141}142143public static VMOption findHeapDumpOnOomOption() {144return findOption(HEAP_DUMP_ON_OOM);145}146147private static VMOption findOption(String optionName) {148List<VMOption> options = mbean.getDiagnosticOptions();149VMOption found = null;150for (VMOption o : options) {151if (o.getName().equals(optionName)) {152found = o;153break;154}155}156if (found == null) {157throw new RuntimeException("VM option " + optionName +158" not found");159}160return found;161}162}163164165