Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are safe points and safe point polling in context of profiling?

I am facing a situation where I do not see some method calls not being recorded by the VisualVM application. Wanted to find out the reason and came across this answer on SO. The third point mentions about a potential issue of the sampling method(which is the only option that I am seeing enabled probably because I am doing remote profiling). It mentions about safe points in code and safe point polling by code itself. What do these terms mean?

like image 355
Geek Avatar asked Jul 24 '13 16:07

Geek


People also ask

What does safe point mean?

adj. 1 designed to return to a safe condition in the event of a failure or malfunction. 2 (of a nuclear weapon) capable of being deactivated in the event of a failure or accident. 3 unlikely to fail; foolproof.

What is safepoint in Java?

A safepoint is a point in program execution where the state of the program is known and can be examined. Things like registers, memory, etc. For the JVM to completely pause and run tasks (such as GC), all threads must come to a safepoint.

What is async Profiler in Java?

This project is a low overhead sampling profiler for Java that does not suffer from Safepoint bias problem. It features HotSpot-specific APIs to collect stack traces and to track memory allocations. The profiler works with OpenJDK, Oracle JDK and other Java runtimes based on the HotSpot JVM.


2 Answers

The issue of inaccuracy of Java sampling profiler tools and its relation to the safe points is very well discussed in Evaluating the Accuracy of Java Profilers (PLDI'10).

Essentially, Java profilers may produce inaccurate results when sampling due to the fact that the sampling occurs during the safe points. And since occurrence of safe-points can be modified by the compiler, execution of some methods may never by sampled by the profiler. Therefore, the profiler is scheduled to record a sample of the code (the time interval is up) but it must wait for the occurrence of the safe-point. And since the safe-point is e.g. moved around by the compiler, the method that would be ideally sampled is never observed.

As already explained by the previous anwer, a safepoint is an event or a position in the code where compiler interrupts execution in order to execute some internal VM code (for example GC).

The safe-point polling is a method of implementing the safepoint or a safepoint trigger. It means that in the code being executed you regularly check a flag to see if a safe-point execution is required, if yes (due to e.g. GC trigger), the thread is interrupted and the safepoint is executed. See e.g. GC safe-point (or safepoint) and safe-region

like image 70
Aleš Avatar answered Nov 16 '22 04:11

Aleš


This blog post discusses safe points. Basically they are points in the code where the JITter allows interruptions for GC, stack traces etc.

The post also says the safe points, by delaying stack samples, cannot occur in places where you might like them to, and that's a problem.

In my opinion, that's a small problem. The whole reason you take a stack sample (as opposed to just a program-counter sample) is to show you all the call-sites leading to the current state, because those are likely to be much more juicy sources of slowness than whatever the program counter is doing. (If it's doing anything. You might be in the middle of I/O, where the PC is meaningless, but the call-sites are still just as important.) If the stack sample has to wait a few cycles to get to a safe point, all that means is it happens at the end of a block of instructions, not in the middle. If you examine the sample you can still get a good idea what's happening.

I'm hoping profiler writers come to realize they don't need to sweat the small stuff. What's more important is not to miss the big stuff.

like image 23
Mike Dunlavey Avatar answered Nov 16 '22 03:11

Mike Dunlavey