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