44 import sun.tools.attach.HotSpotVirtualMachine;
45
46 public class CheckOrigin {
47
48 private static HotSpotDiagnosticMXBean mbean;
49
50 public static void main(String... args) throws Exception {
51 if (args.length == 0) {
52 // start a process that has options set in a number of different ways
53
54 File flagsFile = File.createTempFile("CheckOriginFlags", null);
55 try (PrintWriter pw =
56 new PrintWriter(new FileWriter(flagsFile))) {
57 pw.println("+PrintSafepointStatistics");
58 }
59
60 ProcessBuilder pb = ProcessTools.
61 createJavaProcessBuilder(
62 "-XX:+UseConcMarkSweepGC", // this will cause UseParNewGC to be FLAG_SET_ERGO
63 "-XX:+PrintGCDetails",
64 "-XX:Flags=" + flagsFile.getAbsolutePath(),
65 "-cp", System.getProperty("test.class.path"),
66 "CheckOrigin",
67 "-runtests");
68
69 Map<String, String> env = pb.environment();
70 env.put("_JAVA_OPTIONS", "-XX:+PrintOopAddress");
71
72 pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
73 pb.redirectError(ProcessBuilder.Redirect.INHERIT);
74 Process p = pb.start();
75 int exit = p.waitFor();
76 System.out.println("sub process exit == " + exit);
77 if (exit != 0) {
78 throw new Exception("Unexpected exit code from subprocess == " + exit);
79 }
80 } else {
81 mbean =
82 ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
83
84 // set a few more options
85 mbean.setVMOption("HeapDumpOnOutOfMemoryError", "true");
86 setOptionUsingAttach("HeapDumpPath", "/a/sample/path");
87
88 // check the origin field for all the options we set
89
90 // Not set, so should be default
91 checkOrigin("ManagementServer", Origin.DEFAULT);
92 // Set on the command line
93 checkOrigin("PrintGCDetails", Origin.VM_CREATION);
94 // Set in _JAVA_OPTIONS
95 checkOrigin("PrintOopAddress", Origin.ENVIRON_VAR);
96 // Set in -XX:Flags file
97 checkOrigin("PrintSafepointStatistics", Origin.CONFIG_FILE);
98 // Set through j.l.m
99 checkOrigin("HeapDumpOnOutOfMemoryError", Origin.MANAGEMENT);
100 // Should be set by the VM, when we set UseConcMarkSweepGC
101 checkOrigin("UseParNewGC", Origin.ERGONOMIC);
102 // Set using attach
103 checkOrigin("HeapDumpPath", Origin.ATTACH_ON_DEMAND);
104 }
105 }
106
107 private static void checkOrigin(String option, Origin origin) throws Exception
108 {
109 Origin o = mbean.getVMOption(option).getOrigin();
110 if (!o.equals(origin)) {
111 throw new Exception("Option '" + option + "' should have origin '" + origin + "' but had '" + o + "'");
112 }
113 System.out.println("Option '" + option + "' verified origin = '" + origin + "'");
114 }
115
|
44 import sun.tools.attach.HotSpotVirtualMachine;
45
46 public class CheckOrigin {
47
48 private static HotSpotDiagnosticMXBean mbean;
49
50 public static void main(String... args) throws Exception {
51 if (args.length == 0) {
52 // start a process that has options set in a number of different ways
53
54 File flagsFile = File.createTempFile("CheckOriginFlags", null);
55 try (PrintWriter pw =
56 new PrintWriter(new FileWriter(flagsFile))) {
57 pw.println("+PrintSafepointStatistics");
58 }
59
60 ProcessBuilder pb = ProcessTools.
61 createJavaProcessBuilder(
62 "-XX:+UseConcMarkSweepGC", // this will cause UseParNewGC to be FLAG_SET_ERGO
63 "-XX:+PrintGCDetails",
64 "-XX:+UseCerealGC", // Should be ignored.
65 "-XX:Flags=" + flagsFile.getAbsolutePath(),
66 "-cp", System.getProperty("test.class.path"),
67 "CheckOrigin",
68 "-runtests");
69
70 Map<String, String> env = pb.environment();
71 // "UseCMSGC" should be ignored.
72 env.put("_JAVA_OPTIONS", "-XX:+PrintOopAddress -XX:+UseCMSGC");
73 // "UseGOneGC" should be ignored.
74 env.put("JAVA_TOOL_OPTIONS", "-XX:+IgnoreUnrecognizedVMOptions "
75 + "-XX:+PrintVMOptions -XX:+UseGOneGC");
76
77 pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
78 pb.redirectError(ProcessBuilder.Redirect.INHERIT);
79 Process p = pb.start();
80 int exit = p.waitFor();
81 System.out.println("sub process exit == " + exit);
82 if (exit != 0) {
83 throw new Exception("Unexpected exit code from subprocess == " + exit);
84 }
85 } else {
86 mbean =
87 ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
88
89 // set a few more options
90 mbean.setVMOption("HeapDumpOnOutOfMemoryError", "true");
91 setOptionUsingAttach("HeapDumpPath", "/a/sample/path");
92
93 // check the origin field for all the options we set
94
95 // Not set, so should be default
96 checkOrigin("ManagementServer", Origin.DEFAULT);
97 // Set on the command line
98 checkOrigin("PrintGCDetails", Origin.VM_CREATION);
99 // Set in _JAVA_OPTIONS
100 checkOrigin("PrintOopAddress", Origin.ENVIRON_VAR);
101 // Set in JAVA_TOOL_OPTIONS
102 checkOrigin("IgnoreUnrecognizedVMOptions", Origin.ENVIRON_VAR);
103 checkOrigin("PrintVMOptions", Origin.ENVIRON_VAR);
104 // Set in -XX:Flags file
105 checkOrigin("PrintSafepointStatistics", Origin.CONFIG_FILE);
106 // Set through j.l.m
107 checkOrigin("HeapDumpOnOutOfMemoryError", Origin.MANAGEMENT);
108 // Should be set by the VM, when we set UseConcMarkSweepGC
109 checkOrigin("UseParNewGC", Origin.ERGONOMIC);
110 // Set using attach
111 checkOrigin("HeapDumpPath", Origin.ATTACH_ON_DEMAND);
112 }
113 }
114
115 private static void checkOrigin(String option, Origin origin) throws Exception
116 {
117 Origin o = mbean.getVMOption(option).getOrigin();
118 if (!o.equals(origin)) {
119 throw new Exception("Option '" + option + "' should have origin '" + origin + "' but had '" + o + "'");
120 }
121 System.out.println("Option '" + option + "' verified origin = '" + origin + "'");
122 }
123
|