1 /* 2 * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8028994 27 * @author Staffan Larsen 28 * @library /test/lib 29 * @modules jdk.attach/sun.tools.attach 30 * jdk.management 31 * @run main CheckOrigin 32 */ 33 34 import com.sun.management.HotSpotDiagnosticMXBean; 35 import com.sun.management.VMOption; 36 import com.sun.management.VMOption.Origin; 37 import com.sun.tools.attach.VirtualMachine; 38 import java.io.File; 39 import java.io.FileWriter; 40 import java.io.InputStream; 41 import java.io.PrintWriter; 42 import java.lang.management.ManagementFactory; 43 import java.util.Map; 44 import jdk.test.lib.process.ProcessTools; 45 import sun.tools.attach.HotSpotVirtualMachine; 46 47 public class CheckOrigin { 48 49 private static HotSpotDiagnosticMXBean mbean; 50 51 public static void main(String... args) throws Exception { 52 if (args.length == 0) { 53 // start a process that has options set in a number of different ways 54 55 File flagsFile = File.createTempFile("CheckOriginFlags", null); 56 try (PrintWriter pw = 57 new PrintWriter(new FileWriter(flagsFile))) { 58 pw.println("+PrintVMQWaitTime"); 59 } 60 61 ProcessBuilder pb = ProcessTools. 62 createJavaProcessBuilder( 63 "--add-exports", "jdk.attach/sun.tools.attach=ALL-UNNAMED", 64 "-XX:+UseG1GC", // this will cause MaxNewSize to be FLAG_SET_ERGO 65 "-XX:+UseCodeAging", 66 "-XX:+UseCerealGC", // Should be ignored. 67 "-XX:Flags=" + flagsFile.getAbsolutePath(), 68 "-Djdk.attach.allowAttachSelf", 69 "-cp", System.getProperty("test.class.path"), 70 "CheckOrigin", 71 "-runtests"); 72 73 Map<String, String> env = pb.environment(); 74 env.put("_JAVA_OPTIONS", "-XX:+CheckJNICalls"); 75 // "UseGOneGC" should be ignored. 76 env.put("JAVA_TOOL_OPTIONS", "-XX:+IgnoreUnrecognizedVMOptions " 77 + "-XX:+PrintVMOptions -XX:+UseGOneGC"); 78 79 pb.redirectOutput(ProcessBuilder.Redirect.INHERIT); 80 pb.redirectError(ProcessBuilder.Redirect.INHERIT); 81 Process p = pb.start(); 82 int exit = p.waitFor(); 83 System.out.println("sub process exit == " + exit); 84 if (exit != 0) { 85 throw new Exception("Unexpected exit code from subprocess == " + exit); 86 } 87 } else { 88 mbean = 89 ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class); 90 91 // set a few more options 92 mbean.setVMOption("HeapDumpOnOutOfMemoryError", "true"); 93 setOptionUsingAttach("HeapDumpPath", "/a/sample/path"); 94 95 // check the origin field for all the options we set 96 97 // Not set, so should be default 98 checkOrigin("ManagementServer", Origin.DEFAULT); 99 // Set on the command line 100 checkOrigin("UseCodeAging", Origin.VM_CREATION); 101 // Set in _JAVA_OPTIONS 102 checkOrigin("CheckJNICalls", Origin.ENVIRON_VAR); 103 // Set in JAVA_TOOL_OPTIONS 104 checkOrigin("IgnoreUnrecognizedVMOptions", Origin.ENVIRON_VAR); 105 checkOrigin("PrintVMOptions", Origin.ENVIRON_VAR); 106 // Set in -XX:Flags file 107 checkOrigin("PrintVMQWaitTime", Origin.CONFIG_FILE); 108 // Set through j.l.m 109 checkOrigin("HeapDumpOnOutOfMemoryError", Origin.MANAGEMENT); 110 // Should be set by the VM, when we set UseG1GC 111 checkOrigin("MaxNewSize", Origin.ERGONOMIC); 112 // Set using attach 113 checkOrigin("HeapDumpPath", Origin.ATTACH_ON_DEMAND); 114 } 115 } 116 117 private static void checkOrigin(String option, Origin origin) throws Exception 118 { 119 Origin o = mbean.getVMOption(option).getOrigin(); 120 if (!o.equals(origin)) { 121 throw new Exception("Option '" + option + "' should have origin '" + origin + "' but had '" + o + "'"); 122 } 123 System.out.println("Option '" + option + "' verified origin = '" + origin + "'"); 124 } 125 126 // use attach to set a manageable vm option 127 private static void setOptionUsingAttach(String option, String value) throws Exception { 128 HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine.attach(ProcessTools.getProcessId()+""); 129 InputStream in = vm.setFlag(option, value); 130 System.out.println("Result from setting '" + option + "' to '" + value + "' using attach:"); 131 drain(vm, in); 132 System.out.println("-- end -- "); 133 } 134 135 // Read the stream from the target VM until EOF, print to output, then detach 136 private static void drain(VirtualMachine vm, InputStream in) throws Exception { 137 byte b[] = new byte[256]; 138 int n; 139 do { 140 n = in.read(b); 141 if (n > 0) { 142 String s = new String(b, 0, n, "UTF-8"); 143 System.out.print(s); 144 } 145 } while (n > 0); 146 in.close(); 147 vm.detach(); 148 } 149 150 }