Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/memory/gcLocker.hpp
32285 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_MEMORY_GCLOCKER_HPP25#define SHARE_VM_MEMORY_GCLOCKER_HPP2627#include "gc_interface/collectedHeap.hpp"28#include "gc_interface/gcCause.hpp"29#include "memory/genCollectedHeap.hpp"30#include "memory/universe.hpp"31#include "oops/oop.hpp"32#include "runtime/thread.inline.hpp"33#ifdef TARGET_OS_FAMILY_linux34# include "os_linux.inline.hpp"35#endif36#ifdef TARGET_OS_FAMILY_solaris37# include "os_solaris.inline.hpp"38#endif39#ifdef TARGET_OS_FAMILY_windows40# include "os_windows.inline.hpp"41#endif42#ifdef TARGET_OS_FAMILY_bsd43# include "os_bsd.inline.hpp"44#endif4546// The direct lock/unlock calls do not force a collection if an unlock47// decrements the count to zero. Avoid calling these if at all possible.4849class GC_locker: public AllStatic {50private:51// The _jni_lock_count keeps track of the number of threads that are52// currently in a critical region. It's only kept up to date when53// _needs_gc is true. The current value is computed during54// safepointing and decremented during the slow path of GC_locker55// unlocking.56static volatile jint _jni_lock_count; // number of jni active instances.57static volatile bool _needs_gc; // heap is filling, we need a GC58// note: bool is typedef'd as jint59static volatile bool _doing_gc; // unlock_critical() is doing a GC60static uint _total_collections; // value for _gc_locker collection6162#ifdef ASSERT63// This lock count is updated for all operations and is used to64// validate the jni_lock_count that is computed during safepoints.65static volatile jint _debug_jni_lock_count;66#endif6768// At a safepoint, visit all threads and count the number of active69// critical sections. This is used to ensure that all active70// critical sections are exited before a new one is started.71static void verify_critical_count() NOT_DEBUG_RETURN;7273static void jni_lock(JavaThread* thread);74static void jni_unlock(JavaThread* thread);7576static bool is_active_internal() {77verify_critical_count();78return _jni_lock_count > 0;79}8081public:82// Accessors83static bool is_active() {84assert(SafepointSynchronize::is_at_safepoint(), "only read at safepoint");85return is_active_internal();86}87static bool needs_gc() { return _needs_gc; }8889// Shorthand90static bool is_active_and_needs_gc() {91// Use is_active_internal since _needs_gc can change from true to92// false outside of a safepoint, triggering the assert in93// is_active.94return needs_gc() && is_active_internal();95}9697// In debug mode track the locking state at all times98static void increment_debug_jni_lock_count() {99#ifdef ASSERT100assert(_debug_jni_lock_count >= 0, "bad value");101Atomic::inc(&_debug_jni_lock_count);102#endif103}104static void decrement_debug_jni_lock_count() {105#ifdef ASSERT106assert(_debug_jni_lock_count > 0, "bad value");107Atomic::dec(&_debug_jni_lock_count);108#endif109}110111// Set the current lock count112static void set_jni_lock_count(int count) {113_jni_lock_count = count;114verify_critical_count();115}116117// Sets _needs_gc if is_active() is true. Returns is_active().118static bool check_active_before_gc();119120// Return true if the designated collection is a GCLocker request121// that should be discarded. Returns true if cause == GCCause::_gc_locker122// and the given total collection value indicates a collection has been123// done since the GCLocker request was made.124static bool should_discard(GCCause::Cause cause, uint total_collections);125126// Stalls the caller (who should not be in a jni critical section)127// until needs_gc() clears. Note however that needs_gc() may be128// set at a subsequent safepoint and/or cleared under the129// JNICritical_lock, so the caller may not safely assert upon130// return from this method that "!needs_gc()" since that is131// not a stable predicate.132static void stall_until_clear();133134// The following two methods are used for JNI critical regions.135// If we find that we failed to perform a GC because the GC_locker136// was active, arrange for one as soon as possible by allowing137// all threads in critical regions to complete, but not allowing138// other critical regions to be entered. The reasons for that are:139// 1) a GC request won't be starved by overlapping JNI critical140// region activities, which can cause unnecessary OutOfMemory errors.141// 2) even if allocation requests can still be satisfied before GC locker142// becomes inactive, for example, in tenured generation possibly with143// heap expansion, those allocations can trigger lots of safepointing144// attempts (ineffective GC attempts) and require Heap_lock which145// slow down allocations tremendously.146//147// Note that critical regions can be nested in a single thread, so148// we must allow threads already in critical regions to continue.149//150// JNI critical regions are the only participants in this scheme151// because they are, by spec, well bounded while in a critical region.152//153// Each of the following two method is split into a fast path and a154// slow path. JNICritical_lock is only grabbed in the slow path.155// _needs_gc is initially false and every java thread will go156// through the fast path, which simply increments or decrements the157// current thread's critical count. When GC happens at a safepoint,158// GC_locker::is_active() is checked. Since there is no safepoint in159// the fast path of lock_critical() and unlock_critical(), there is160// no race condition between the fast path and GC. After _needs_gc161// is set at a safepoint, every thread will go through the slow path162// after the safepoint. Since after a safepoint, each of the163// following two methods is either entered from the method entry and164// falls into the slow path, or is resumed from the safepoints in165// the method, which only exist in the slow path. So when _needs_gc166// is set, the slow path is always taken, till _needs_gc is cleared.167static void lock_critical(JavaThread* thread);168static void unlock_critical(JavaThread* thread);169170static address needs_gc_address() { return (address) &_needs_gc; }171};172173174// A No_GC_Verifier object can be placed in methods where one assumes that175// no garbage collection will occur. The destructor will verify this property176// unless the constructor is called with argument false (not verifygc).177//178// The check will only be done in debug mode and if verifygc true.179180class No_GC_Verifier: public StackObj {181friend class Pause_No_GC_Verifier;182183protected:184bool _verifygc;185unsigned int _old_invocations;186187public:188#ifdef ASSERT189No_GC_Verifier(bool verifygc = true);190~No_GC_Verifier();191#else192No_GC_Verifier(bool verifygc = true) {}193~No_GC_Verifier() {}194#endif195};196197// A Pause_No_GC_Verifier is used to temporarily pause the behavior198// of a No_GC_Verifier object. If we are not in debug mode or if the199// No_GC_Verifier object has a _verifygc value of false, then there200// is nothing to do.201202class Pause_No_GC_Verifier: public StackObj {203private:204No_GC_Verifier * _ngcv;205206public:207#ifdef ASSERT208Pause_No_GC_Verifier(No_GC_Verifier * ngcv);209~Pause_No_GC_Verifier();210#else211Pause_No_GC_Verifier(No_GC_Verifier * ngcv) {}212~Pause_No_GC_Verifier() {}213#endif214};215216217// A No_Safepoint_Verifier object will throw an assertion failure if218// the current thread passes a possible safepoint while this object is219// instantiated. A safepoint, will either be: an oop allocation, blocking220// on a Mutex or JavaLock, or executing a VM operation.221//222// If StrictSafepointChecks is turned off, it degrades into a No_GC_Verifier223//224class No_Safepoint_Verifier : public No_GC_Verifier {225friend class Pause_No_Safepoint_Verifier;226227private:228bool _activated;229Thread *_thread;230public:231#ifdef ASSERT232No_Safepoint_Verifier(bool activated = true, bool verifygc = true ) :233No_GC_Verifier(verifygc),234_activated(activated) {235_thread = Thread::current();236if (_activated) {237_thread->_allow_allocation_count++;238_thread->_allow_safepoint_count++;239}240}241242~No_Safepoint_Verifier() {243if (_activated) {244_thread->_allow_allocation_count--;245_thread->_allow_safepoint_count--;246}247}248#else249No_Safepoint_Verifier(bool activated = true, bool verifygc = true) : No_GC_Verifier(verifygc){}250~No_Safepoint_Verifier() {}251#endif252};253254// A Pause_No_Safepoint_Verifier is used to temporarily pause the255// behavior of a No_Safepoint_Verifier object. If we are not in debug256// mode then there is nothing to do. If the No_Safepoint_Verifier257// object has an _activated value of false, then there is nothing to258// do for safepoint and allocation checking, but there may still be259// something to do for the underlying No_GC_Verifier object.260261class Pause_No_Safepoint_Verifier : public Pause_No_GC_Verifier {262private:263No_Safepoint_Verifier * _nsv;264265public:266#ifdef ASSERT267Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv)268: Pause_No_GC_Verifier(nsv) {269270_nsv = nsv;271if (_nsv->_activated) {272_nsv->_thread->_allow_allocation_count--;273_nsv->_thread->_allow_safepoint_count--;274}275}276277~Pause_No_Safepoint_Verifier() {278if (_nsv->_activated) {279_nsv->_thread->_allow_allocation_count++;280_nsv->_thread->_allow_safepoint_count++;281}282}283#else284Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv)285: Pause_No_GC_Verifier(nsv) {}286~Pause_No_Safepoint_Verifier() {}287#endif288};289290// A SkipGCALot object is used to elide the usual effect of gc-a-lot291// over a section of execution by a thread. Currently, it's used only to292// prevent re-entrant calls to GC.293class SkipGCALot : public StackObj {294private:295bool _saved;296Thread* _t;297298public:299#ifdef ASSERT300SkipGCALot(Thread* t) : _t(t) {301_saved = _t->skip_gcalot();302_t->set_skip_gcalot(true);303}304305~SkipGCALot() {306assert(_t->skip_gcalot(), "Save-restore protocol invariant");307_t->set_skip_gcalot(_saved);308}309#else310SkipGCALot(Thread* t) { }311~SkipGCALot() { }312#endif313};314315// JRT_LEAF currently can be called from either _thread_in_Java or316// _thread_in_native mode. In _thread_in_native, it is ok317// for another thread to trigger GC. The rest of the JRT_LEAF318// rules apply.319class JRT_Leaf_Verifier : public No_Safepoint_Verifier {320static bool should_verify_GC();321public:322#ifdef ASSERT323JRT_Leaf_Verifier();324~JRT_Leaf_Verifier();325#else326JRT_Leaf_Verifier() {}327~JRT_Leaf_Verifier() {}328#endif329};330331// A No_Alloc_Verifier object can be placed in methods where one assumes that332// no allocation will occur. The destructor will verify this property333// unless the constructor is called with argument false (not activated).334//335// The check will only be done in debug mode and if activated.336// Note: this only makes sense at safepoints (otherwise, other threads may337// allocate concurrently.)338339class No_Alloc_Verifier : public StackObj {340private:341bool _activated;342343public:344#ifdef ASSERT345No_Alloc_Verifier(bool activated = true) {346_activated = activated;347if (_activated) Thread::current()->_allow_allocation_count++;348}349350~No_Alloc_Verifier() {351if (_activated) Thread::current()->_allow_allocation_count--;352}353#else354No_Alloc_Verifier(bool activated = true) {}355~No_Alloc_Verifier() {}356#endif357};358359#endif // SHARE_VM_MEMORY_GCLOCKER_HPP360361362