Sun JVM Attach API是Sun JVM中的一套非标准的可以连接到JVM上的API,从JDK6开始引入,除了Solaris平台的Sun JVM支持远程的Attach,在其他平台都只允许Attach到本地的JVM上。
一、Sun JVM Attach API功能上非常简单,仅提供了如下几个功能:
- 列出当前所有的JVM实例描述(知道JDK工具jps吗,列出所有的java进程的pid)
- Attach到其中一个JVM上,建立通信管道
- 让目标JVM加载Agent(还记得JVM TI吧,运行后加载的Agent)
二、通过几个实例简单地了解一下如上的几个功能
1.列出当前所有的JVM实例描述
List<VirtualMachineDescriptor> list = VirtualMachine.list();
for (VirtualMachineDescriptor vmd : list)
{
System.out.println("pid:" + vmd.id() + ":" + vmd.displayName());
}
JConsole的连接界面中的本地部分就是通过这个API来做的
2.Attach到特定进程的JVM上,并加载Agent,我们看看jconsole是怎么做的(见sun.tools.jconsole. LocalVirtualMachine)
1)首先是Attach到JVM上
VirtualMachine virtualmachine = VirtualMachine.attach(pid);
2)然后是加载Agent
String javaHome = virtualmachine.getSystemProperties().getProperty("java.home");
String agentPath = javaHome + File.separator + "jre" + File.separator + "lib" + File.separator + "management-agent.jar");
File file = new File(agentPath);
if(!file.exists())
{
agentPath = javaHome + File.separator + "lib" + File.separator + "management-agent.jar";
file = new File(agentPath);
if(!file.exists())
throw new IOException("Management agent not found");
}
}
agentPath = file.getCanonicalPath();
try
{
virtualmachine.loadAgent(agentPath, "com.sun.management.jmxremote");
}
catch(AgentLoadException e)
{
throw new IOException(e);
}
catch(AgentInitializationException agentinitializationexception)
{
throw new IOException(e);
}
Properties properties = virtualmachine.getAgentProperties();
address = (String)properties.get("com.sun.management.jmxremote.localConnectorAddress");
virtualmachine.detach();
三、我们来看一下底层是如何处理Attach的,不同的平台处理方式当然是不一样的,我们看看Windows平台是如何处理的
首先Attach API定义了AttachProvider,我们当然可以通过实现不同的 AttachProvider来提供不同的Attach方式,当然这不是一件简单的事情,因为实际上是需要JVM支持的。以Windows平台为例,实现了WindowsAttachProvider,我们看看其中最主要的attach方法
public VirtualMachine attachVirtualMachine(String vmid)
throws AttachNotSupportedException, IOException
{
checkAttachPermission();
testAttachable(vmid);
return new WindowsVirtualMachine(this, vmid);
}
WindowsVirtualMachine在构造函数中先连接上目标JVM
WindowsVirtualMachine(AttachProvider attachprovider, String s)
throws AttachNotSupportedException, IOException
{
super(attachprovider, s);
…
hProcess = openProcess(i);
…
}
WindowsVirtualMachine继承HotSpotVirtualMachine,先看看HotSpotVirtualMachine的loadAgent方法
private void loadAgentLibrary(String s, boolean flag, String s1)
throws AgentLoadException, AgentInitializationException, IOException
{
InputStream inputstream = execute("load", new Object[] {
s, flag ? "true" : "false", s1
});
...
}
再看看WindowsVirtualMachine的execute,基本上调用的各种方法都是native方法,只能大概地猜测与JVM进程之间使用管道进行了一个通信
transient InputStream execute(String s, Object aobj[])
throws AgentLoadException, IOException
{
int i = (new Random()).nextInt();
String pipe ="\\\\.\\pipe\\javatool" + i;
long l = createPipe(pipe);
…
try
{
enqueue(hProcess, stub, s, pipe, aobj);
connectPipe(l);
PipedInputStream pipedinputstream = new PipedInputStream(l);
…
}
catch(IOException ioexception)
{
closePipe(l);
throw ioexception;
}
}