Path: blob/master/test/hotspot/jtreg/serviceability/dcmd/vm/SetVMFlagTest.java
41153 views
/*1* Copyright (c) 2015, 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*/2223import jdk.test.lib.Platform;24import jdk.test.lib.process.OutputAnalyzer;25import jdk.test.lib.dcmd.CommandExecutor;26import jdk.test.lib.dcmd.JMXExecutor;27import org.testng.annotations.Test;28import static org.testng.Assert.*;2930/*31* @test32* @bug 805489033* @summary Test of VM.set_flag diagnostic command34* @modules java.base/jdk.internal.misc35* @library /test/lib36* @run testng SetVMFlagTest37*/3839public class SetVMFlagTest {40private static final String MANAGEABLE_PATTERN = "\\s*bool\\s+(\\S+)\\s+[\\:]?=\\s+" +41"(.*?)\\s+\\{manageable\\}";42private static final String IMMUTABLE_PATTERN = "\\s*uintx\\s+(\\S+)\\s+[\\:]?=\\s+" +43"(.*?)\\s+\\{product\\}";4445public void run(CommandExecutor executor) {46setMutableFlag(executor);47setMutableFlagWithInvalidValue(executor);48setImmutableFlag(executor);49setNonExistingFlag(executor);50setStringFlag(executor);51}5253@Test54public void jmx() {55run(new JMXExecutor());56}5758private void setMutableFlagInternal(CommandExecutor executor, String flag,59boolean val, boolean isNumeric) {60String strFlagVal;61if (isNumeric) {62strFlagVal = val ? "1" : "0";63} else {64strFlagVal = val ? "true" : "false";65}6667OutputAnalyzer out = executor.execute("VM.set_flag " + flag + " " + strFlagVal);68out.stderrShouldBeEmpty();6970out = getAllFlags(executor);7172String newFlagVal = out.firstMatch(MANAGEABLE_PATTERN.replace("(\\S+)", flag), 1);7374assertNotEquals(newFlagVal, val ? "1" : "0");75}7677private void setMutableFlag(CommandExecutor executor) {78OutputAnalyzer out = getAllFlags(executor);79String flagName = out.firstMatch(MANAGEABLE_PATTERN, 1);80String flagVal = out.firstMatch(MANAGEABLE_PATTERN, 2);8182System.out.println("### Setting a mutable flag '" + flagName + "'");8384if (flagVal == null) {85System.err.println(out.getOutput());86throw new Error("Can not find a boolean manageable flag");87}8889Boolean blnVal = Boolean.parseBoolean(flagVal);90setMutableFlagInternal(executor, flagName, !blnVal, true);91setMutableFlagInternal(executor, flagName, blnVal, false);92}9394private void setMutableFlagWithInvalidValue(CommandExecutor executor) {95OutputAnalyzer out = getAllFlags(executor);96String flagName = out.firstMatch(MANAGEABLE_PATTERN, 1);97String flagVal = out.firstMatch(MANAGEABLE_PATTERN, 2);9899System.out.println("### Setting a mutable flag '" + flagName + "' to an invalid value");100101if (flagVal == null) {102System.err.println(out.getOutput());103throw new Error("Can not find a boolean manageable flag");104}105106// a boolean flag accepts only 0/1 as its value107out = executor.execute("VM.set_flag " + flagName + " unexpected_value");108out.stderrShouldBeEmpty();109out.stdoutShouldContain("flag value must be a boolean (1/0 or true/false)");110111out = getAllFlags(executor);112113String newFlagVal = out.firstMatch(MANAGEABLE_PATTERN.replace("(\\S+)", flagName), 1);114115assertEquals(newFlagVal, flagVal);116}117118private void setImmutableFlag(CommandExecutor executor) {119OutputAnalyzer out = getAllFlags(executor);120String flagName = out.firstMatch(IMMUTABLE_PATTERN, 1);121String flagVal = out.firstMatch(IMMUTABLE_PATTERN, 2);122123System.out.println("### Setting an immutable flag '" + flagName + "'");124125if (flagVal == null) {126System.err.println(out.getOutput());127throw new Error("Can not find an immutable uintx flag");128}129130Long numVal = Long.parseLong(flagVal);131132out = executor.execute("VM.set_flag " + flagName + " " + (numVal + 1));133out.stderrShouldBeEmpty();134out.stdoutShouldContain("only 'writeable' flags can be set");135136out = getAllFlags(executor);137138String newFlagVal = out.firstMatch(IMMUTABLE_PATTERN.replace("(\\S+)", flagName), 1);139140assertEquals(newFlagVal, flagVal);141}142143private void setNonExistingFlag(CommandExecutor executor) {144String unknownFlag = "ThisIsUnknownFlag";145System.out.println("### Setting a non-existing flag '" + unknownFlag + "'");146OutputAnalyzer out = executor.execute("VM.set_flag " + unknownFlag + " 1");147out.stderrShouldBeEmpty();148out.stdoutShouldContain("flag " + unknownFlag + " does not exist");149}150151private void setStringFlag(CommandExecutor executor) {152// Today we don't have any manageable flags of the string type in the product build,153// so we can only test DummyManageableStringFlag in the debug build.154if (!Platform.isDebugBuild()) {155return;156}157158String flag = "DummyManageableStringFlag";159String toValue = "DummyManageableStringFlag_Is_Set_To_Hello";160161System.out.println("### Setting a string flag '" + flag + "'");162OutputAnalyzer out = executor.execute("VM.set_flag " + flag + " " + toValue);163out.stderrShouldBeEmpty();164165out = getAllFlags(executor);166out.stdoutShouldContain(toValue);167}168169private OutputAnalyzer getAllFlags(CommandExecutor executor) {170return executor.execute("VM.flags -all", true);171}172}173174175