< prev index next >

test/jdk/com/sun/management/HotSpotDiagnosticMXBean/CheckOrigin.java

Print this page




  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("+PrintSafepointStatistics");
  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");


  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("PrintSafepointStatistics", 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 {




  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");


  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 {


< prev index next >