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