1 /* 2 * Copyright (c) 2013, 2018, 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 * @comment Graal does not support CMS 29 * @requires !vm.graal.enabled 30 * @library /test/lib 31 * @modules jdk.attach/sun.tools.attach 32 * jdk.management 33 * @run main CheckOrigin 34 */ 35 36 import com.sun.management.HotSpotDiagnosticMXBean; 37 import com.sun.management.VMOption; 38 import com.sun.management.VMOption.Origin; 39 import com.sun.tools.attach.VirtualMachine; 40 import java.io.File; 41 import java.io.FileWriter; 42 import java.io.InputStream; 43 import java.io.PrintWriter; 44 import java.lang.management.ManagementFactory; 45 import java.util.Map; 46 import jdk.test.lib.process.ProcessTools; 47 import sun.tools.attach.HotSpotVirtualMachine; 48 49 public class CheckOrigin { 50 51 private static HotSpotDiagnosticMXBean mbean; 52 53 public static void main(String... args) throws Exception { 54 if (args.length == 0) { 55 // start a process that has options set in a number of different ways 56 57 File flagsFile = File.createTempFile("CheckOriginFlags", null); 58 try (PrintWriter pw = 59 new PrintWriter(new FileWriter(flagsFile))) { 60 pw.println("+PrintVMQWaitTime"); 61 } 62 63 ProcessBuilder pb = ProcessTools. 64 createJavaProcessBuilder( 65 "--add-exports", "jdk.attach/sun.tools.attach=ALL-UNNAMED", 66 "-XX:+UseConcMarkSweepGC", // this will cause MaxNewSize to be FLAG_SET_ERGO 67 "-XX:+UseCodeAging", 68 "-XX:+UseCerealGC", // Should be ignored. 69 "-XX:Flags=" + flagsFile.getAbsolutePath(), 70 "-Djdk.attach.allowAttachSelf", 71 "-cp", System.getProperty("test.class.path"), 72 "CheckOrigin", 73 "-runtests"); 74 75 Map<String, String> env = pb.environment(); 76 // "UseCMSGC" should be ignored. 77 env.put("_JAVA_OPTIONS", "-XX:+CheckJNICalls -XX:+UseCMSGC"); 78 // "UseGOneGC" should be ignored. 79 env.put("JAVA_TOOL_OPTIONS", "-XX:+IgnoreUnrecognizedVMOptions " 80 + "-XX:+PrintVMOptions -XX:+UseGOneGC"); 81 82 pb.redirectOutput(ProcessBuilder.Redirect.INHERIT); 83 pb.redirectError(ProcessBuilder.Redirect.INHERIT); 84 Process p = pb.start(); 85 int exit = p.waitFor(); 86 System.out.println("sub process exit == " + exit); 87 if (exit != 0) { 88 throw new Exception("Unexpected exit code from subprocess == " + exit); 89 } 90 } else { 91 mbean = 92 ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class); 93 94 // set a few more options 95 mbean.setVMOption("HeapDumpOnOutOfMemoryError", "true"); 96 setOptionUsingAttach("HeapDumpPath", "/a/sample/path"); 97 98 // check the origin field for all the options we set 99 100 // Not set, so should be default 101 checkOrigin("ManagementServer", Origin.DEFAULT); 102 // Set on the command line 103 checkOrigin("UseCodeAging", Origin.VM_CREATION); 104 // Set in _JAVA_OPTIONS 105 checkOrigin("CheckJNICalls", Origin.ENVIRON_VAR); 106 // Set in JAVA_TOOL_OPTIONS 107 checkOrigin("IgnoreUnrecognizedVMOptions", Origin.ENVIRON_VAR); 108 checkOrigin("PrintVMOptions", Origin.ENVIRON_VAR); 109 // Set in -XX:Flags file 110 checkOrigin("PrintVMQWaitTime", Origin.CONFIG_FILE); 111 // Set through j.l.m 112 checkOrigin("HeapDumpOnOutOfMemoryError", Origin.MANAGEMENT); 113 // Should be set by the VM, when we set UseConcMarkSweepGC 114 checkOrigin("MaxNewSize", Origin.ERGONOMIC); 115 // Set using attach 116 checkOrigin("HeapDumpPath", Origin.ATTACH_ON_DEMAND); 117 } 118 } 119 120 private static void checkOrigin(String option, Origin origin) throws Exception 121 { 122 Origin o = mbean.getVMOption(option).getOrigin(); 123 if (!o.equals(origin)) { 124 throw new Exception("Option '" + option + "' should have origin '" + origin + "' but had '" + o + "'"); 125 } 126 System.out.println("Option '" + option + "' verified origin = '" + origin + "'"); 127 } 128 129 // use attach to set a manageable vm option 130 private static void setOptionUsingAttach(String option, String value) throws Exception { 131 HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine.attach(ProcessTools.getProcessId()+""); 132 InputStream in = vm.setFlag(option, value); 133 System.out.println("Result from setting '" + option + "' to '" + value + "' using attach:"); 134 drain(vm, in); 135 System.out.println("-- end -- "); 136 } 137 138 // Read the stream from the target VM until EOF, print to output, then detach 139 private static void drain(VirtualMachine vm, InputStream in) throws Exception { 140 byte b[] = new byte[256]; 141 int n; 142 do { 143 n = in.read(b); 144 if (n > 0) { 145 String s = new String(b, 0, n, "UTF-8"); 146 System.out.print(s); 147 } 148 } while (n > 0); 149 in.close(); 150 vm.detach(); 151 } 152 153 }