changeset 57954:9544e515be4e

8196729: Add jstatd option to specify RMI connector port Reviewed-by: cjplummer, sspitsyn
author dtitov
date Thu, 06 Feb 2020 11:23:51 -0800
parents 8d8916159b62
children adc5b0998235
files src/jdk.jstatd/share/classes/sun/tools/jstatd/Jstatd.java src/jdk.jstatd/share/classes/sun/tools/jstatd/RemoteHostImpl.java test/jdk/sun/tools/jstatd/JstatdTest.java test/jdk/sun/tools/jstatd/TestJstatdRmiPort.java test/jdk/sun/tools/jstatd/TestJstatdUsage.java
diffstat 5 files changed, 110 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.jstatd/share/classes/sun/tools/jstatd/Jstatd.java	Thu Feb 06 13:08:14 2020 -0500
+++ b/src/jdk.jstatd/share/classes/sun/tools/jstatd/Jstatd.java	Thu Feb 06 11:23:51 2020 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -48,7 +48,7 @@
     private static RemoteHost remoteHost;
 
     private static void printUsage() {
-        System.err.println("usage: jstatd [-nr] [-p port] [-n rminame]\n" +
+        System.err.println("usage: jstatd [-nr] [-p port] [-r rmiport] [-n rminame]\n" +
                            "       jstatd -?|-h|--help");
     }
 
@@ -75,6 +75,7 @@
     @SuppressWarnings("deprecation") // Use of RMISecurityManager
     public static void main(String[] args) {
         String rminame = null;
+        int rmiPort = 0;
         int argc = 0;
 
         for ( ; (argc < args.length) && (args[argc].startsWith("-")); argc++) {
@@ -98,6 +99,17 @@
                   }
                   port = Integer.parseInt(args[argc]);
                 }
+            } else if (arg.startsWith("-r")) {
+                if (arg.compareTo("-r") != 0) {
+                    rmiPort = Integer.parseInt(arg.substring(2));
+                } else {
+                    argc++;
+                    if (argc >= args.length) {
+                        printUsage();
+                        System.exit(1);
+                    }
+                    rmiPort = Integer.parseInt(args[argc]);
+                }
             } else if (arg.startsWith("-n")) {
                 if (arg.compareTo("-n") != 0) {
                     rminame = arg.substring(2);
@@ -139,9 +151,9 @@
         try {
             // use 1.5.0 dynamically generated subs.
             System.setProperty("java.rmi.server.ignoreSubClasses", "true");
-            remoteHost = new RemoteHostImpl();
+            remoteHost = new RemoteHostImpl(rmiPort);
             RemoteHost stub = (RemoteHost) UnicastRemoteObject.exportObject(
-                    remoteHost, 0);
+                    remoteHost, rmiPort);
             bind(name.toString(), stub);
             System.out.println("jstatd started (bound to " + name.toString() + ")");
             System.out.flush();
--- a/src/jdk.jstatd/share/classes/sun/tools/jstatd/RemoteHostImpl.java	Thu Feb 06 13:08:14 2020 -0500
+++ b/src/jdk.jstatd/share/classes/sun/tools/jstatd/RemoteHostImpl.java	Thu Feb 06 11:23:51 2020 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2004, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -51,8 +51,14 @@
     private MonitoredHost monitoredHost;
     private Set<Integer> activeVms;
     private static RemoteVm rvm;
+    private final int rmiPort;
 
     public RemoteHostImpl() throws MonitorException {
+        this(0);
+    }
+
+    public RemoteHostImpl(int rmiPort) throws MonitorException {
+        this.rmiPort = rmiPort;
         try {
             monitoredHost = MonitoredHost.getMonitoredHost("localhost");
         } catch (URISyntaxException e) { }
@@ -78,7 +84,7 @@
             VmIdentifier vmid = new VmIdentifier(vmidStr);
             MonitoredVm mvm = monitoredHost.getMonitoredVm(vmid);
             rvm = new RemoteVmImpl((BufferedMonitoredVm)mvm);
-            stub = (RemoteVm) UnicastRemoteObject.exportObject(rvm, 0);
+            stub = (RemoteVm) UnicastRemoteObject.exportObject(rvm, rmiPort);
         }
         catch (URISyntaxException e) {
             throw new RuntimeException("Malformed VmIdentifier URI: "
--- a/test/jdk/sun/tools/jstatd/JstatdTest.java	Thu Feb 06 13:08:14 2020 -0500
+++ b/test/jdk/sun/tools/jstatd/JstatdTest.java	Thu Feb 06 11:23:51 2020 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -65,10 +65,13 @@
     private static final String JPS_OUTPUT_REGEX = "^\\d+\\s*.*";
 
     private boolean useDefaultPort = true;
+    private boolean useDefaultRmiPort = true;
     private String port;
+    private String rmiPort;
     private String serverName;
     private Long jstatdPid;
     private boolean withExternalRegistry = false;
+    private boolean useShortCommandSyntax = false;
 
     public void setServerName(String serverName) {
         this.serverName = serverName;
@@ -78,6 +81,10 @@
         this.useDefaultPort = useDefaultPort;
     }
 
+    public void setUseDefaultRmiPort(boolean useDefaultRmiPort) {
+        this.useDefaultRmiPort = useDefaultRmiPort;
+    }
+
     public void setWithExternalRegistry(boolean withExternalRegistry) {
         this.withExternalRegistry = withExternalRegistry;
     }
@@ -245,6 +252,7 @@
      *
      * jstatd -J-XX:+UsePerfData -J-Djava.security.policy=all.policy
      * jstatd -J-XX:+UsePerfData -J-Djava.security.policy=all.policy -p port
+     * jstatd -J-XX:+UsePerfData -J-Djava.security.policy=all.policy -p port -r rmiport
      * jstatd -J-XX:+UsePerfData -J-Djava.security.policy=all.policy -n serverName
      * jstatd -J-XX:+UsePerfData -J-Djava.security.policy=all.policy -p port -n serverName
      */
@@ -257,22 +265,31 @@
                 "Security policy " + policy.getAbsolutePath() + " does not exist or not a file");
         launcher.addVMArg("-Djava.security.policy=" + policy.getAbsolutePath());
         if (port != null) {
-            launcher.addToolArg("-p");
-            launcher.addToolArg(port);
+            addToolArg(launcher,"-p", port);
+        }
+        if (rmiPort != null) {
+            addToolArg(launcher,"-r", rmiPort);
         }
         if (serverName != null) {
-            launcher.addToolArg("-n");
-            launcher.addToolArg(serverName);
+            addToolArg(launcher,"-n", serverName);
         }
         if (withExternalRegistry) {
             launcher.addToolArg("-nr");
         }
-
         String[] cmd = launcher.getCommand();
         log("Start jstatd", cmd);
         return cmd;
     }
 
+    private void addToolArg(JDKToolLauncher launcher, String name, String value) {
+        if (useShortCommandSyntax) {
+            launcher.addToolArg(name + value);
+        } else {
+            launcher.addToolArg(name);
+            launcher.addToolArg(value);
+        }
+    }
+
     private ProcessThread tryToSetupJstatdProcess() throws Throwable {
         ProcessThread jstatdThread = new ProcessThread("Jstatd-Thread",
                 JstatdTest::isJstadReady, getJstatdCmd());
@@ -300,6 +317,12 @@
     }
 
     public void doTest() throws Throwable {
+        runTest(false);
+        runTest(true);
+    }
+
+    private void runTest(boolean useShortSyntax) throws Throwable {
+        useShortCommandSyntax = useShortSyntax;
         if (useDefaultPort) {
             verifyNoRmiRegistryOnDefaultPort();
         }
@@ -311,6 +334,10 @@
                     port = String.valueOf(Utils.getFreePort());
                 }
 
+                if (!useDefaultRmiPort) {
+                    rmiPort = String.valueOf(Utils.getFreePort());
+                }
+
                 if (withExternalRegistry) {
                     Registry registry = startRegistry();
                     if (registry == null) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/sun/tools/jstatd/TestJstatdRmiPort.java	Thu Feb 06 11:23:51 2020 -0800
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ *
+ * @library /test/lib
+ *
+ * @build JstatdTest JstatGCUtilParser
+ * @run main/timeout=60 TestJstatdRmiPort
+ */
+public class TestJstatdRmiPort {
+
+    public static void main(String[] args) throws Throwable {
+        testRmiPort();
+        testRegistryAndRmiPorts();
+    }
+
+    private static void testRmiPort() throws Throwable {
+        JstatdTest test = new JstatdTest();
+        test.setUseDefaultRmiPort(false);
+        test.doTest();
+    }
+
+    private static void testRegistryAndRmiPorts() throws Throwable {
+        JstatdTest test = new JstatdTest();
+        test.setUseDefaultPort(false);
+        test.setUseDefaultRmiPort(false);
+        test.doTest();
+    }
+}
--- a/test/jdk/sun/tools/jstatd/TestJstatdUsage.java	Thu Feb 06 13:08:14 2020 -0500
+++ b/test/jdk/sun/tools/jstatd/TestJstatdUsage.java	Thu Feb 06 11:23:51 2020 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -46,7 +46,7 @@
         ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
         OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
 
-        output.shouldContain("usage: jstatd [-nr] [-p port] [-n rminame]");
+        output.shouldContain("usage: jstatd [-nr] [-p port] [-r rmiport] [-n rminame]");
         output.shouldHaveExitValue(0);
     }