Path: blob/master/test/jdk/com/sun/management/HotSpotDiagnosticMXBean/CheckOrigin.java
41153 views
/*1* Copyright (c) 2013, 2019, 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 802899426* @author Staffan Larsen27* @library /test/lib28* @modules jdk.attach/sun.tools.attach29* jdk.management30* @run main CheckOrigin31*/3233import com.sun.management.HotSpotDiagnosticMXBean;34import com.sun.management.VMOption;35import com.sun.management.VMOption.Origin;36import com.sun.tools.attach.VirtualMachine;37import java.io.File;38import java.io.FileWriter;39import java.io.InputStream;40import java.io.PrintWriter;41import java.lang.management.ManagementFactory;42import java.util.Map;43import jdk.test.lib.process.ProcessTools;44import sun.tools.attach.HotSpotVirtualMachine;4546public class CheckOrigin {4748private static HotSpotDiagnosticMXBean mbean;4950public static void main(String... args) throws Exception {51if (args.length == 0) {52// start a process that has options set in a number of different ways5354File flagsFile = File.createTempFile("CheckOriginFlags", null);55try (PrintWriter pw =56new PrintWriter(new FileWriter(flagsFile))) {57pw.println("+PrintCodeCache");58}5960ProcessBuilder pb = ProcessTools.61createJavaProcessBuilder(62"--add-exports", "jdk.attach/sun.tools.attach=ALL-UNNAMED",63"-XX:+UseG1GC", // this will cause MaxNewSize to be FLAG_SET_ERGO64"-XX:+UseCodeAging",65"-XX:+UseCerealGC", // Should be ignored.66"-XX:Flags=" + flagsFile.getAbsolutePath(),67"-Djdk.attach.allowAttachSelf",68"-cp", System.getProperty("test.class.path"),69"CheckOrigin",70"-runtests");7172Map<String, String> env = pb.environment();73env.put("_JAVA_OPTIONS", "-XX:+CheckJNICalls");74// "UseGOneGC" should be ignored.75env.put("JAVA_TOOL_OPTIONS", "-XX:+IgnoreUnrecognizedVMOptions "76+ "-XX:+PrintVMOptions -XX:+UseGOneGC");7778pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);79pb.redirectError(ProcessBuilder.Redirect.INHERIT);80Process p = pb.start();81int exit = p.waitFor();82System.out.println("sub process exit == " + exit);83if (exit != 0) {84throw new Exception("Unexpected exit code from subprocess == " + exit);85}86} else {87mbean =88ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);8990// set a few more options91mbean.setVMOption("HeapDumpOnOutOfMemoryError", "true");92setOptionUsingAttach("HeapDumpPath", "/a/sample/path");9394// check the origin field for all the options we set9596// Not set, so should be default97checkOrigin("ManagementServer", Origin.DEFAULT);98// Set on the command line99checkOrigin("UseCodeAging", Origin.VM_CREATION);100// Set in _JAVA_OPTIONS101checkOrigin("CheckJNICalls", Origin.ENVIRON_VAR);102// Set in JAVA_TOOL_OPTIONS103checkOrigin("IgnoreUnrecognizedVMOptions", Origin.ENVIRON_VAR);104checkOrigin("PrintVMOptions", Origin.ENVIRON_VAR);105// Set in -XX:Flags file106checkOrigin("PrintCodeCache", Origin.CONFIG_FILE);107// Set through j.l.m108checkOrigin("HeapDumpOnOutOfMemoryError", Origin.MANAGEMENT);109// Should be set by the VM, when we set UseG1GC110checkOrigin("MaxNewSize", Origin.ERGONOMIC);111// Set using attach112checkOrigin("HeapDumpPath", Origin.ATTACH_ON_DEMAND);113}114}115116private static void checkOrigin(String option, Origin origin) throws Exception117{118Origin o = mbean.getVMOption(option).getOrigin();119if (!o.equals(origin)) {120throw new Exception("Option '" + option + "' should have origin '" + origin + "' but had '" + o + "'");121}122System.out.println("Option '" + option + "' verified origin = '" + origin + "'");123}124125// use attach to set a manageable vm option126private static void setOptionUsingAttach(String option, String value) throws Exception {127HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine.attach(ProcessTools.getProcessId()+"");128InputStream in = vm.setFlag(option, value);129System.out.println("Result from setting '" + option + "' to '" + value + "' using attach:");130drain(vm, in);131System.out.println("-- end -- ");132}133134// Read the stream from the target VM until EOF, print to output, then detach135private static void drain(VirtualMachine vm, InputStream in) throws Exception {136byte b[] = new byte[256];137int n;138do {139n = in.read(b);140if (n > 0) {141String s = new String(b, 0, n, "UTF-8");142System.out.print(s);143}144} while (n > 0);145in.close();146vm.detach();147}148149}150151152