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