3

We have a java program. I would like to track entry and returns of a few functions by calling appropriate profiler functions on the entry and returns of those functions.

Tracking a function entry is simple so won't be part of this discussion. However, return is a bit messy as we would have to add profiler code at every return points, and hence looking for suggestions how best we can do without cluttering the code.

In C++, it was easy, just create a local instance of a small trace class at entry of the function and the constructor and destructor of that trace class would take care of calling profiler function. However, java has no destructor (finalize won't work) so that approach is out of question.

Any suggestions/tips?

mesibo
  • 3,970
  • 6
  • 25
  • 43
  • Use a profiler... but be aware of it disturbing the result. Don't trust its timing result on small methods. When you need to profile a few methods only, you can use try-finally. Otherwise, consider AOP (aspect oriented programming) tools. Anyway, I'd suggest looking at https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java - it's only remotely related, but shows that things are more complicated than with an AOT (ahead of time) compiler. – maaartinus Dec 12 '18 at 00:06

2 Answers2

2

Use a try/finally block over the whole method body:

// handle entry
try {
    // function body
} finally {
    // handle return
}
Ingo Kegel
  • 46,523
  • 10
  • 71
  • 102
2

If you are on JDK 11 and want to measure the time a method execute with very low overhead you could create a Flight Recorder event.

@StackTrace(false)
static class StopWatch extends jdk.jfr.Event {
}

StopWatch s = new StopWatch();
try {
  s.begin();
  ...
} finally {
  s.commit();
}

To record, start the JVM with:

java -XX:StartFlightRecording:filename=dump.jfr

and the recording will be written when the JVM exists. It can then be opened in JDK Mission Control. That said, adding instrumentation to hot methods may skew the results and sampling is usually a better approach, in which case you can just use Flight Recorder as is without the added code.

Kire Haglin
  • 6,569
  • 22
  • 27
  • Hello @Kire Haglin thanks a lot it's a life saver. May I ask if this method can output the whole stack that can be used/view in the flame chart to see which child method consumed most time? Would be great if you can help me check this question https://stackoverflow.com/questions/69211369/how-to-profile-a-single-java-spring-method-run – qkhanhpro Sep 16 '21 at 16:09
  • No, you will need a sampling profiler. You can increase the sampling rate with JFR so you are more likely see child methods, but it may not be sufficient to help you. You can set sampling rate in the JMC Recording Wizard or Window -> Template Manager if you are running from command line. See "Method Profiling". – Kire Haglin Sep 17 '21 at 00:20
  • In JDK 17, you can do: $ java -XX:StartFlightRecording:method-profiling=max,filename=dump.jfr ... – Kire Haglin Sep 17 '21 at 00:24