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