Re: Extend jcmd to java application level

Denghui Dong denghui.ddh at alibaba-inc.com
Sat Oct 9 06:14:44 UTC 2021


Hi,

Thank you for the response.

I just made a PoC for this extension: https://github.com/D-D-H/jdk/commit/eb2a13b7ba8bd3e6d048491c8a579b179d2cef0d

Here is the test code:
```
import sun.management.cmd.CmdMeta;
import sun.management.cmd.Command;
import sun.management.cmd.ParamMeta;
import sun.management.cmd.Factory;

import java.io.PrintWriter;

public class Test {

  public static void main(String... args) throws Exception {
    Factory.register(Echo.class);
    Thread.sleep(60000);
  }
}

@CmdMeta(name = "My.Echo")
class Echo implements Command {
    public Echo() {}

    @ParamMeta(name = "text", isMandatory = true)
    String text;

    @Override
    public void execute(PrintWriter out) {
      out.println(text);
    }
}
```
Verify steps:
1. javac --add-exports java.management/sun.management.cmd=ALL-UNNAMED Test.java
1. java --add-exports java.management/sun.management.cmd=ALL-UNNAMED Test
3. jcmd <pid> My.Echo text=message

At present, it is only the most basic implementation, mainly referring to the implementation of dmcd in the VM layer. and there are many places that need more careful design and implementation, such as security and timeout

If you think this extension is useful, I'm happy to continue this work.

Thanks,
Denghui
------------------------------------------------------------------
From:董登辉(卓昂) <denghui.ddh at alibaba-inc.com>
Send Time:2021年10月7日(星期四) 21:58
To:serviceability-dev <serviceability-dev at openjdk.java.net>; hotspot-runtime-dev at openjdk.java.net <hotspot-runtime-dev at openjdk.java.net>
Subject:Extend jcmd to java application level

Hi team,

The `jcmd` command can be used to call some built-in diagnostic commands in vm.
Can we consider extending it to the java layer like perf data, so that Java developers can
customize their diagnostic commands and then call them through `jcmd`?

One application scenario I can think of for this extension is that some statistical information
may be collected in a java application. Triggering the output of this statistical information through
the `jcmd` command seems to me relative to other mechanisms that trigger output (such as through
an HTTP service, or periodic Printing) is more convenient.

Any input is appreciated.

Thanks,
Denghui Dong
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/serviceability-dev/attachments/20211009/8beff854/attachment.htm>


More information about the serviceability-dev mailing list