jvm-profiling-tools / perf-map-agent

A java agent to generate method mappings to use with the linux `perf` tool
GNU General Public License v2.0
1.64k stars 260 forks source link

java.io.IOException: No such process #91

Closed MartinDeLaTorre closed 1 year ago

MartinDeLaTorre commented 1 year ago

Hello, I was trying to follow along Wenfeng's tutorial: https://wenfeng-gao.github.io/post/profile-java-program-with-bcc-tool/

but I ended up getting this error from Line 18 of the final script:

[root@PF1N5MWV /]# docker exec --user "${JAVA_UID}":"${JAVA_GID}" "${CTN_ID}" bash -c "cd /tmp/perf-map-agent && java -cp /tmp/perf-map-agent/attach-main.jar:${JAVA_HOME}/lib/tools.jar net.virtualvoid.perf.AttachOnce ${JAVA_PID}"
Exception in thread "main" java.io.IOException: No such process
        at jdk.attach/sun.tools.attach.VirtualMachineImpl.sendQuitTo(Native Method)
        at jdk.attach/sun.tools.attach.VirtualMachineImpl.<init>(VirtualMachineImpl.java:83)
        at jdk.attach/sun.tools.attach.AttachProviderImpl.attachVirtualMachine(AttachProviderImpl.java:58)
        at jdk.attach/com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:207)
        at net.virtualvoid.perf.AttachOnce.loadAgent(AttachOnce.java:38)
        at net.virtualvoid.perf.AttachOnce.main(AttachOnce.java:34)

I just don't understand why is says "No such process" when the container is running and the java spring boot app is also running.

I noticed that "tools.jar" was on the classpath (line 18 of Wenfeng's final script). tools.jar was removed in java 9, so I tried downgrading to java 8 to see if that would fix it, but I ran into problems trying to find a way to compile "libperfmap.so" and "attach-main.jar" for java 8, and I ran out of time for today.

Any clues as to why the "No such process" error happens? I was getting the PID by doing: "docker top java-example"

"java-example" is the container for the spring boot application I was trying to profile. You can try it with:

docker run --name java-example --rm --detach martindelatorre/p3-backend

So do I need java 8? I also found a related issue for using java 9+: https://github.com/jvm-profiling-tools/perf-map-agent/issues/42 I was hoping to avoid downgrading because that would mean I would only be able to profile Java 8 applications, and there isn't an easy way to install java 8 with apt-get (there used to be a way to use apt-get to install java 8, but it seems oracle changed some stuff around and now the .deb packages don't work anymore. I think yum might still support java 8 though.)

or is there some other reason that the process couldn't be found? or did I just mess up the PID?


Update:

I managed to find a java 8 jdk to build the perf-map-agent, but after building everything with java 8 I still got the same error.

[root@PF1N5MWV usr]# echo $JAVA_HOME
/usr/local/openjdk-8
[root@PF1N5MWV usr]# echo $JAVA_BIN
/usr/local/openjdk-8/bin
[root@PF1N5MWV usr]# echo $JAVA_PID
21469
[root@PF1N5MWV usr]# echo $JAVA_UID
0
[root@PF1N5MWV usr]# echo $JAVA_GID
0
[root@PF1N5MWV usr]# docker top java-example
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                21469               21450               0                   19:42               ?                   00:00:35            java -jar e-commerce-1.0.jar
[root@PF1N5MWV usr]# docker exec --user "${JAVA_UID}":"${JAVA_GID}" "java-example" bash -c "cd /tmp/perf-map-agent && java -cp /tmp/perf-map-agent/attach-main.jar:${JAVA_HOME}/lib/tools.jar net.virtualvoid.perf.AttachOnce ${JAVA_PID}"
Exception in thread "main" java.io.IOException: No such process
        at sun.tools.attach.LinuxVirtualMachine.sendQuitTo(Native Method)
        at sun.tools.attach.LinuxVirtualMachine.<init>(LinuxVirtualMachine.java:91)
        at sun.tools.attach.LinuxAttachProvider.attachVirtualMachine(LinuxAttachProvider.java:63)
        at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:208)
        at net.virtualvoid.perf.AttachOnce.loadAgent(AttachOnce.java:38)
        at net.virtualvoid.perf.AttachOnce.main(AttachOnce.java:34)
[root@PF1N5MWV usr]#

this is what the command evaluates to: docker exec --user "0":"0" "java-example" bash -c "cd /tmp/perf-map-agent && java -cp /tmp/perf-map-agent/attach-main.jar:/usr/local/openjdk-8/lib/tools.jar net.virtualvoid.perf.AttachOnce 21469"

MartinDeLaTorre commented 1 year ago

Apparently docker exec java-example ps -e -o pid,command (returned PID of 1) does not show the same PIDs as CTN_ID=java-example; docker top $CTN_ID (returned PID of 25706).

I was using 25706 for everything.