5

I'm interested in whether it is possible to get start and end time in nanoseconds for every or most of user methods in profiling with JFR program?

I know that it is possible to do with code instrumentation, but I would like to leverage advantage of sampling profiler to reduce impact on actual program execution.

I have a sense that it should be possible as I believe JFR need this info for hot methods percentage evaluation, but don't know how to get that time. Could someone help?

user2529617
  • 103
  • 8

1 Answers1

2

It's not possible to get that using JFR.

Since it's a sampling profiler, it will only collect what method that was on top of the stack when the sample was taken. You can get a high precision timestamp when it occurred, but it will not tell you for how long time the method executed.

Here is a program that shows how the histogram is built.

try (RecordingFile f = new RecordingFile(Paths.get("recording.jfr"))) {
   Map<String, SimpleEntry<String, Integer>> histogram = new HashMap<>();
   int total = 0;
   while (f.hasMoreEvents()) {
     RecordedEvent event = f.readEvent();
     if (event.getEventType().getName().endsWith(".ExecutionSample")) {
       RecordedStackTrace s = event.getStackTrace();
       if (s != null) {
         RecordedFrame topFrame= s.getFrames().get(0);
         if (topFrame.isJavaFrame())  {
           RecordedMethod method = topFrame.getMethod();
           String methodName = method.getType().getName() + "#" + method.getName() + " " + method.getDescriptor();
           Entry entry = histogram.computeIfAbsent(methodName, u -> new SimpleEntry<String, Integer>(methodName, 0));
           entry.setValue(entry.getValue() + 1);
           total++;
         }
       }
     }
   }
   List<SimpleEntry<String, Integer>> entries = new ArrayList<>(histogram.values());
   entries.sort((u, v) -> v.getValue().compareTo(u.getValue()));
   for (SimpleEntry<String, Integer> c : entries) {
     System.out.printf("%2.0f%% %s\n", 100 * (float) c.getValue() / total, c.getKey());
   }
   System.out.println("\nSample count: " + total);
 } 
Kire Haglin
  • 6,569
  • 22
  • 27