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