5

The citation comes from: http://blog.ragozin.info/2012/10/safepoints-in-hotspot-jvm.html

Safepoint status check itself is implemented in very cunning way. Normal memory variable check would require expensive memory barriers. Though, safepoint check is implemented as memory reads a barrier. Then safepoint is required, JVM unmaps page with that address provoking page fault on application thread (which is handled by JVM’s handler). This way, HotSpot maintains its JITed code CPU pipeline friendly, yet ensures correct memory semantic (page unmap is forcing memory barrier to processing cores).

I have some doubts:

  1. From what I know page fault is always handled by OS (and, from what I understsand it can handled only by kernel because of security). So, what autor does mean?
  2. The second is very similar to the first one: How JVM is able to unmap a page?
2
  • Presumably the author meant "segfault", when the page fault hander finds there's no mapping for a page. Regarding unmapping, see munmap; similar calls are available on Windows. Sep 24, 2017 at 20:38
  • are you doubting the correctness of the statements made in that blog post? or do you accept them and just want explanations how this works?
    – the8472
    Sep 25, 2017 at 9:12

1 Answer 1

8
  1. OS handles page faults in the first place. If the reason of this fault is an illegal memory access attempted by an application, OS delivers the appropriate signal to the application, typically SIGSEGV.

    By default, SIGSEGV kills an application. However, an application may install its own signal handler. That's what JVM does. It receives SIGSEGV, and if it sees that the signal is caused by safepoint poll instruction, JVM suspends current thread until the safepoint operation is over.

  2. A process may unmap pages by calling munmap(). But in this particular case JVM does not unmap the page, but rather changes its protection state by calling mprotect() with PROT_NONE.

4
  • So, the JVM is cunning :). It intentionally puts access to the special region of memory in the safepoints. In that way, if mprotect(PROT_NONE) was called, the thread is interrupted by OS, and JVM decides whether it should run garbage collector. It means that safepoints must be inserted in the loop to ensure that thread hits to the safepoint, yes.
    – Gilgamesz
    Sep 25, 2017 at 18:59
  • Thanks for your great answer!! :)
    – Gilgamesz
    Sep 25, 2017 at 19:00
  • Does unmapping a page causes write it on a disk?
    – Gilgamesz
    Sep 25, 2017 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.