Skip to content

Commit

Permalink
8196729: Add jstatd option to specify RMI connector port
Browse files Browse the repository at this point in the history
Reviewed-by: cjplummer, sspitsyn
  • Loading branch information
Daniil Titov committed Feb 6, 2020
1 parent 87031d4 commit 26b642f
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 14 deletions.
20 changes: 16 additions & 4 deletions src/jdk.jstatd/share/classes/sun/tools/jstatd/Jstatd.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -48,7 +48,7 @@ public class Jstatd {
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");
}

Expand All @@ -75,6 +75,7 @@ static void bind(String name, RemoteHost remoteHost)
@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++) {
Expand All @@ -98,6 +99,17 @@ public static void main(String[] args) {
}
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);
Expand Down Expand Up @@ -139,9 +151,9 @@ public static void main(String[] args) {
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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -51,8 +51,14 @@ public class RemoteHostImpl implements RemoteHost, HostListener {
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) { }
Expand All @@ -78,7 +84,7 @@ public RemoteVm attachVm(int lvmid, String mode)
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: "
Expand Down
39 changes: 33 additions & 6 deletions test/jdk/sun/tools/jstatd/JstatdTest.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -65,10 +65,13 @@ public final class JstatdTest {
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;
Expand All @@ -78,6 +81,10 @@ public void setUseDefaultPort(boolean useDefaultPort) {
this.useDefaultPort = useDefaultPort;
}

public void setUseDefaultRmiPort(boolean useDefaultRmiPort) {
this.useDefaultRmiPort = useDefaultRmiPort;
}

public void setWithExternalRegistry(boolean withExternalRegistry) {
this.withExternalRegistry = withExternalRegistry;
}
Expand Down Expand Up @@ -245,6 +252,7 @@ private void cleanUpThread(ProcessThread thread) throws Throwable {
*
* 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
*/
Expand All @@ -257,22 +265,31 @@ private String[] getJstatdCmd() throws Exception {
"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());
Expand Down Expand Up @@ -300,6 +317,12 @@ private static boolean isJstadReady(String line) {
}

public void doTest() throws Throwable {
runTest(false);
runTest(true);
}

private void runTest(boolean useShortSyntax) throws Throwable {
useShortCommandSyntax = useShortSyntax;
if (useDefaultPort) {
verifyNoRmiRegistryOnDefaultPort();
}
Expand All @@ -311,6 +334,10 @@ public void doTest() throws Throwable {
port = String.valueOf(Utils.getFreePort());
}

if (!useDefaultRmiPort) {
rmiPort = String.valueOf(Utils.getFreePort());
}

if (withExternalRegistry) {
Registry registry = startRegistry();
if (registry == null) {
Expand Down
51 changes: 51 additions & 0 deletions test/jdk/sun/tools/jstatd/TestJstatdRmiPort.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
4 changes: 2 additions & 2 deletions test/jdk/sun/tools/jstatd/TestJstatdUsage.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -46,7 +46,7 @@ private static void testUsage(String option) throws Exception {
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);
}

Expand Down

0 comments on commit 26b642f

Please sign in to comment.