Attaching to running JVM

Questions about YourKit Java Profiler
Post Reply
robelee
Posts: 2
Joined: Thu Nov 20, 2014 3:19 pm

Attaching to running JVM

Post by robelee »

Hi,

Goal: capture/record only at certain times

What I've done:
1) Bring up JVM with yjp at startup. On the client side, I can attach to the remote host and I can do things such as look at live data, capture-save-download-open the snapshot file etc. That is all working fine. However, when we do this, the analysis is "polluted" with data that we are not interested (startup, initialization etc). What we would like to do is something similar to JProfile whereby one can attach to a running JVM and then start capturing the profile.
2) I've also tried to bring up JVM without yjp, and then do an attach. It didn't work. Here's what I got:

FYI:
>: uname -a
Linux sip01.sip-prf4-001.orn-svc01.uci.altus.bblabs.rim.net 3.2.0-70-virtual #105-Ubuntu SMP Wed Sep 24 20:06:46 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

and when I tried the following:

>: ./yjp.sh -attach 16581
[YourKit Java Profiler 2014 build 14112] Log file: /root/.yjp/log/yjp-18935.log
Attaching to process 16581 using default options
com.yourkit.runtime.PresentableException: com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file: target process not responding or HotSpot VM not loaded
at com.yourkit.h.u.p.a(a:102)
at com.yourkit.h.u.c.a(a:200)
at com.yourkit.Main1.int(a:183)
at com.yourkit.Main1.entry(a:271)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.yourkit.Main$2.run(a:8)

Attach to a running JVM failed.

Solution: start JVM with the profiler agent instead of attaching it to a running JVM:
http://www.yourkit.com/docs/java/help/r ... ofiler.jsp

NOTE: I have saved the yjp log file and can post/send it, if you are interested.

Q1: Is it possible to attach to a running JVM? I can see where this is a useful ability (eg, we are running for a long period of time, and something happens and we are now interested in seeing what is happening)
Q2: Let's say that the answer to Q1 is "NO, it is not possible" and I must have the yjp agent running at startup. Is there a way to clear all the data and then start the capture? Specifically, when I do a capture and view the results, it looks like the "Hot Spots" is showing "everything" as in, from when yjp/JVM started.

Thanks,
Robert
robelee
Posts: 2
Joined: Thu Nov 20, 2014 3:19 pm

Re: Attaching to running JVM

Post by robelee »

NOTE: I have also tried the following, with the same results:

>: ./yjp.sh -attach 16581 "-agentpath:/home/local/yjp-2014-build-14112/bin/linux-x86-64/libyjpagent.so=delay=10000,sessionname=Tomcat"
[YourKit Java Profiler 2014 build 14112] Log file: /root/.yjp/log/yjp-22409.log
Attaching to process 16581 using options -agentpath:/home/local/yjp-2014-build-14112/bin/linux-x86-64/libyjpagent.so=delay=10000,sessionname=Tomcat
com.yourkit.runtime.PresentableException: com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file: target process not responding or HotSpot VM not loaded
at com.yourkit.h.u.p.a(a:102)
at com.yourkit.h.u.c.a(a:200)
at com.yourkit.Main1.int(a:183)
at com.yourkit.Main1.entry(a:271)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.yourkit.Main$2.run(a:8)

Attach to a running JVM failed.

Solution: start JVM with the profiler agent instead of attaching it to a running JVM:
http://www.yourkit.com/docs/java/help/r ... ofiler.jsp
Anton Katilin
Posts: 6172
Joined: Wed Aug 11, 2004 8:37 am

Re: Attaching to running JVM

Post by Anton Katilin »

Hi Robert
Goal: capture/record only at certain times
This is possible and easy to achieve. Please find my comments below.
1)... That is all working fine. However, when we do this, the analysis is "polluted" with data that we are not interested (startup, initialization etc).
Could you please clarify what profiling results you mean.

CPU profiling, either sampling or tracing, starts only when you explicitly instruct the profiler to do it.
http://www.yourkit.com/docs/java/help/cpu_intro.jsp
Note: by default it is not started. You can start via a startup option or start/stop in UI, via API, triggers.

If you mean CPU telemetry, it is always collected:
http://www.yourkit.com/docs/java/help/cpu_telemetry.jsp
It's light weight thus adds no overhead.

Thread telemetry (includes states and stacks) may add overhead but you can turn it off:
http://www.yourkit.com/docs/java/help/threads.jsp
What we would like to do is something similar to JProfile whereby one can attach to a running JVM and then start capturing the profile.
This is of course possible.
>: ./yjp.sh -attach 16581
[YourKit Java Profiler 2014 build 14112] Log file: /root/.yjp/log/yjp-18935.log
Attaching to process 16581 using default options
com.yourkit.runtime.PresentableException: com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file: target process not responding or HotSpot VM not loaded
You use the attach mode:
http://www.yourkit.com/docs/java/help/attach_agent.jsp

yjp.sh -attach is just a frontend to corresponding JVM API which allows to attach given agent library to a running JVM by its PID. The error which you see comes from JVM, it's not the profiler's one.

In this particular case I think the problem may be with access rights. According to the log file location /root/.yjp you run yjp.sh as root. Does application you want to attach also run as root? Please note that the attaching process (yjp.sh -attach) should run under the same user as the JVM process you attach to.
Q1: Is it possible to attach to a running JVM?
Yes.
Q2: .... Is there a way to clear all the data and then start the capture? Specifically, when I do a capture and view the results, it looks like the "Hot Spots" is showing "everything" as in, from when yjp/JVM started.
Please see my comment above. Which Hot Spots do you mean? As I wrote, CPU profiling starts only when you tell it to start.
NOTE: I have also tried the following, with the same results:

>: ./yjp.sh -attach 16581 "-agentpath:/home/local/yjp-2014-build-14112/bin/linux-x86-64/libyjpagent.so=delay=10000,sessionname=Tomcat"
[YourKit Java Profiler 2014 build 14112] Log file: /root/.yjp/log/yjp-22409.log
Attaching to process 16581 using options -agentpath:/home/local/yjp-2014-build-14112/bin/linux-x86-64/libyjpagent.so=delay=10000,sessionname=Tomcat
com.yourkit.runtime.PresentableException: com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file: target process not responding or HotSpot VM not loaded
The error is same as above. The startup options are applied by the agent when it loads. But since the attach did not happen, the agent did not start loading and the options processing was not performed. You could specify any string in "" instead with the same effect.

Anyway, please note that you incorrectly specified the options:
http://www.yourkit.com/docs/java/help/s ... ptions.jsp

It should be './yjp.sh -attach 16581 "delay=10000,sessionname=Tomcat"' instead, i.e. only the part after "agent name =".

The -agentpath is a JVM option, it's a thing like -Xmx or -Dproperty=value. It is specified when you launch Java via a command line.
When you attach to a running JVM, there is no command line. It has already started. The agent options should be passed "naked".

Best regards,
Anton
Post Reply