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