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