Skip to content

Commit

Permalink
stop using internal APIs such as jps as they are not supported in new…
Browse files Browse the repository at this point in the history
…er JVMs move compilation to JDK 17
  • Loading branch information
Haim Yadid committed Nov 24, 2022
1 parent 9f441df commit f1f122b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 329 deletions.
11 changes: 2 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
</developers>

<properties>
<java.compiler.source>1.8</java.compiler.source>
<java.compiler.target>1.8</java.compiler.target>
<java.compiler.source>17</java.compiler.source>
<java.compiler.target>17</java.compiler.target>
</properties>

<build>
Expand Down Expand Up @@ -92,13 +92,6 @@
<artifactId>reflections</artifactId>
<version>0.9.12</version>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>7.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>

</project>
65 changes: 8 additions & 57 deletions src/main/java/com/performizeit/jmxsupport/JMXConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
*/
package com.performizeit.jmxsupport;

import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
Expand All @@ -45,12 +41,10 @@
public class JMXConnection {
String host;
String port;
String userName = "";
String userPassword = "";
String userName;
String userPassword;
JMXServiceURL serviceURL;
private static final String CONNECTOR_ADDRESS =
"com.sun.management.jmxremote.localConnectorAddress";
private String connectURL;
private final String connectURL;
private boolean originalThreadContentionEnabledValue;

public boolean isOriginalThreadContentionEnabledValue() {
Expand All @@ -61,40 +55,8 @@ public void setOriginalThreadContentionEnabledValue(boolean originalThreadConten
this.originalThreadContentionEnabledValue = originalThreadContentionEnabledValue;
}

public JMXConnection(String pid) throws AttachNotSupportedException, IOException, AgentLoadException, AgentInitializationException {
addToolsJar();
connectURL = pid;
// attach to the target application
com.sun.tools.attach.VirtualMachine vm =
com.sun.tools.attach.VirtualMachine.attach(pid);
JMXServiceURL u;
try {
// get the connector address
String connectorAddress =
vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);

// no connector address, so we start the JMX agent
if (connectorAddress == null) {
String agent = vm.getSystemProperties().getProperty("java.home")
+ File.separator + "lib" + File.separator
+ "management-agent.jar";
vm.loadAgent(agent);

// agent is started, get the connector address
connectorAddress =
vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);
}

// establish connection to connector server
// System.out.println(connectorAddress);
serviceURL = new JMXServiceURL(connectorAddress);

} finally {
vm.detach();
}
}

public JMXConnection(String serverUrl, String uName, String passwd) throws MalformedURLException {
public JMXConnection(String serverUrl, String uName, String passwd) throws MalformedURLException {
userName = uName;
userPassword = passwd;
host = serverUrl;
Expand Down Expand Up @@ -150,15 +112,7 @@ public long getUptime() {

l = (Long) getServerConnection().getAttribute(JMXConnection.RUNTIME, "Uptime");

} catch (MBeanException ex) {
Logger.getLogger(JMXConnection.class.getName()).log(Level.SEVERE, null, ex);
} catch (AttributeNotFoundException ex) {
Logger.getLogger(JMXConnection.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstanceNotFoundException ex) {
Logger.getLogger(JMXConnection.class.getName()).log(Level.SEVERE, null, ex);
} catch (ReflectionException ex) {
Logger.getLogger(JMXConnection.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
} catch (MBeanException | AttributeNotFoundException | InstanceNotFoundException | ReflectionException | IOException ex) {
Logger.getLogger(JMXConnection.class.getName()).log(Level.SEVERE, null, ex);
}
return l;
Expand All @@ -176,24 +130,21 @@ boolean isUseAuthentication() {
public CompositeData[] getThreads(long[] thIds, int stackTraceEntriesNo) throws Exception {
String[] signature = {"[J", "int"};
Object[] params = {thIds, stackTraceEntriesNo};
CompositeData[] threads = (CompositeData[]) server.invoke(THREADING, "getThreadInfo", params, signature);
return threads;
return (CompositeData[]) server.invoke(THREADING, "getThreadInfo", params, signature);
}

public long[] getThreadsCPU(long[] thIds) throws Exception {

String[] signature = {"[J"};
Object[] params = {thIds};
long[] threadsCPU = (long[]) server.invoke(THREADING, "getThreadCpuTime", params, signature);
return threadsCPU;
return (long[]) server.invoke(THREADING, "getThreadCpuTime", params, signature);
}

public long getThreadCPU(long thId) throws Exception {

String[] signature = {"long"};
Object[] params = {thId};
long threadCPU = (Long) server.invoke(THREADING, "getThreadCpuTime", params, signature);
return threadCPU;
return (long) (Long) server.invoke(THREADING, "getThreadCpuTime", params, signature);

}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
import com.performizeit.plumbing.GeneratorHandler;

import javax.management.openmbean.CompositeData;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import static com.performizeit.mjprof.parser.ThreadInfoProps.*;

// host:port or pid , freq,period ,user,pass
@SuppressWarnings("unused")
@Plugin(name = "jmx", params = {@Param("host:port|MainClass|pid"),
@Plugin(name = "jmx", params = {@Param("host:port"),
@Param(type = int.class, value = "count", optional = true, defaultValue = "1"),
@Param(type = int.class, value = "sleep", optional = true, defaultValue = "5000"),
@Param(value = "username", optional = true),
Expand All @@ -29,37 +28,20 @@ public class JmxDataSourcePlugin implements DataSource, GeneratorHandler<ThreadD
protected final int count;
protected int iter = 0;

protected String hostPortPid;
protected JMXConnection server = null;
protected String hostPort;
protected JMXConnection server;
protected long lastIterTime = 0;

public JmxDataSourcePlugin(String hostPortPid, int count, int sleep, String user, String pass) {
this.hostPortPid = hostPortPid;
public JmxDataSourcePlugin(String hostPort, int count, int sleep, String user, String pass) {
this.hostPort = hostPort;
this.count = count;
this.sleep = sleep;
user = user.trim().isEmpty() ? null : user;
pass = pass.trim().isEmpty() ? null : pass;
try {
try {
Integer.parseInt(hostPortPid);

server = new JMXConnection(hostPortPid);
} catch (NumberFormatException e) {
if (!hostPortPid.contains(":")) {
int pid = JPSUtil.lookupProcessId(hostPortPid);
if (pid == -1) {
System.err.println("Process id for main class '" + hostPortPid + "' could not be resolved");
System.exit(1);
} else {
server = new JMXConnection(Integer.toString(pid));
}
} else {

server = new JMXConnection(hostPortPid, user, pass);
}
}
server = new JMXConnection(hostPort, user, pass);
} catch (Exception e) {
System.err.println("ERROR: Unable to open JMX connection for" + hostPortPid);
System.err.println("ERROR: Unable to open JMX connection for" + hostPort);
System.exit(1);
server = null;

Expand All @@ -72,7 +54,7 @@ protected ThreadDump getThreadDump() throws Exception {
long iterStart = System.currentTimeMillis();
try {

threadDump.setHeader((new Date()).toString() + "\nThread dump via JMX of process " + hostPortPid);
threadDump.setHeader((new Date()) + "\nThread dump via JMX of process " + hostPort);
long[] threadsIds = server.getThreadIds();


Expand All @@ -99,7 +81,7 @@ protected ThreadDump getThreadDump() throws Exception {
}

protected HashMap<String, Object> getProps(CompositeData thread) {
HashMap<String, Object> props = new HashMap<String, Object>();
HashMap<String, Object> props = new HashMap<>();
props.put(NAME, thread.get("threadName"));
props.put(TID, thread.get("threadId"));
props.put(STATE, thread.get("threadState"));
Expand Down Expand Up @@ -142,4 +124,4 @@ public void sleepBetweenIteration() {

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
public class JmxDataSourcePluginWithCpu extends JmxDataSourcePlugin implements DataSource, GeneratorHandler<ThreadDump> {
protected boolean collectCPU = true;

public JmxDataSourcePluginWithCpu(String hostPortPid, int count, int sleep, String user, String pass) {
super(hostPortPid, count, sleep, user, pass);
public JmxDataSourcePluginWithCpu(String hostPort, int count, int sleep, String user, String pass) {
super(hostPort, count, sleep, user, pass);
}

private double percentDouble(long nom, long denom) {
Expand All @@ -39,7 +39,7 @@ protected ThreadDump getThreadDump() throws Exception {
long iterStart = System.currentTimeMillis();
try {

threadDump.setHeader((new Date()).toString() + "\nThread dump via JMX of process " + hostPortPid);
threadDump.setHeader((new Date()) + "\nThread dump via JMX of process " + hostPort);
long[] threadsIds = server.getThreadIds();
long pre = 0;
long post = 0;
Expand Down Expand Up @@ -91,7 +91,6 @@ protected ThreadDump getThreadDump() throws Exception {

@Override
public void sleepBetweenIteration() {
return;
}

}
}
Loading

0 comments on commit f1f122b

Please sign in to comment.