src/share/vm/memory/gcLocker.hpp

Wed, 15 Feb 2012 10:12:55 -0800

author
never
date
Wed, 15 Feb 2012 10:12:55 -0800
changeset 3571
09d00c18e323
parent 3500
0382d2b469b2
child 3576
ad3b47344802
permissions
-rw-r--r--

7145537: minor tweaks to LogEvents
Reviewed-by: kvn, twisti

     1 /*
     2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 #ifndef SHARE_VM_MEMORY_GCLOCKER_HPP
    26 #define SHARE_VM_MEMORY_GCLOCKER_HPP
    28 #include "gc_interface/collectedHeap.hpp"
    29 #include "memory/genCollectedHeap.hpp"
    30 #include "memory/universe.hpp"
    31 #include "oops/oop.hpp"
    32 #ifdef TARGET_OS_FAMILY_linux
    33 # include "os_linux.inline.hpp"
    34 # include "thread_linux.inline.hpp"
    35 #endif
    36 #ifdef TARGET_OS_FAMILY_solaris
    37 # include "os_solaris.inline.hpp"
    38 # include "thread_solaris.inline.hpp"
    39 #endif
    40 #ifdef TARGET_OS_FAMILY_windows
    41 # include "os_windows.inline.hpp"
    42 # include "thread_windows.inline.hpp"
    43 #endif
    44 #ifdef TARGET_OS_FAMILY_bsd
    45 # include "os_bsd.inline.hpp"
    46 # include "thread_bsd.inline.hpp"
    47 #endif
    49 // The direct lock/unlock calls do not force a collection if an unlock
    50 // decrements the count to zero. Avoid calling these if at all possible.
    52 class GC_locker: public AllStatic {
    53  private:
    54   // The _jni_lock_count keeps track of the number of threads that are
    55   // currently in a critical region.  It's only kept up to date when
    56   // _needs_gc is true.  The current value is computed during
    57   // safepointing and decremented during the slow path of GC_locker
    58   // unlocking.
    59   static volatile jint _jni_lock_count;  // number of jni active instances.
    61   static volatile jint _lock_count;      // number of other active instances
    62   static volatile bool _needs_gc;        // heap is filling, we need a GC
    63                                          // note: bool is typedef'd as jint
    64   static volatile bool _doing_gc;        // unlock_critical() is doing a GC
    66 #ifdef ASSERT
    67   // This lock count is updated for all operations and is used to
    68   // validate the jni_lock_count that is computed during safepoints.
    69   static volatile jint _debug_jni_lock_count;
    70 #endif
    72   // Accessors
    73   static bool is_jni_active() {
    74     assert(_needs_gc, "only valid when _needs_gc is set");
    75     return _jni_lock_count > 0;
    76   }
    78   // At a safepoint, visit all threads and count the number of active
    79   // critical sections.  This is used to ensure that all active
    80   // critical sections are exited before a new one is started.
    81   static void verify_critical_count() NOT_DEBUG_RETURN;
    83   static void jni_lock(JavaThread* thread);
    84   static void jni_unlock(JavaThread* thread);
    86  public:
    87   // Accessors
    88   static bool is_active();
    89   static bool needs_gc()       { return _needs_gc;                        }
    91   // Shorthand
    92   static bool is_active_and_needs_gc() { return needs_gc() && is_active(); }
    94   // In debug mode track the locking state at all times
    95   static void increment_debug_jni_lock_count() {
    96 #ifdef ASSERT
    97     assert(_debug_jni_lock_count >= 0, "bad value");
    98     Atomic::inc(&_debug_jni_lock_count);
    99 #endif
   100   }
   101   static void decrement_debug_jni_lock_count() {
   102 #ifdef ASSERT
   103     assert(_debug_jni_lock_count > 0, "bad value");
   104     Atomic::dec(&_debug_jni_lock_count);
   105 #endif
   106   }
   108   // Set the current lock count
   109   static void set_jni_lock_count(int count) {
   110     _jni_lock_count = count;
   111     verify_critical_count();
   112   }
   114   // Sets _needs_gc if is_active() is true. Returns is_active().
   115   static bool check_active_before_gc();
   117   // Stalls the caller (who should not be in a jni critical section)
   118   // until needs_gc() clears. Note however that needs_gc() may be
   119   // set at a subsequent safepoint and/or cleared under the
   120   // JNICritical_lock, so the caller may not safely assert upon
   121   // return from this method that "!needs_gc()" since that is
   122   // not a stable predicate.
   123   static void stall_until_clear();
   125   // Non-structured GC locking: currently needed for JNI. Use with care!
   126   static void lock();
   127   static void unlock();
   129   // The following two methods are used for JNI critical regions.
   130   // If we find that we failed to perform a GC because the GC_locker
   131   // was active, arrange for one as soon as possible by allowing
   132   // all threads in critical regions to complete, but not allowing
   133   // other critical regions to be entered. The reasons for that are:
   134   // 1) a GC request won't be starved by overlapping JNI critical
   135   //    region activities, which can cause unnecessary OutOfMemory errors.
   136   // 2) even if allocation requests can still be satisfied before GC locker
   137   //    becomes inactive, for example, in tenured generation possibly with
   138   //    heap expansion, those allocations can trigger lots of safepointing
   139   //    attempts (ineffective GC attempts) and require Heap_lock which
   140   //    slow down allocations tremendously.
   141   //
   142   // Note that critical regions can be nested in a single thread, so
   143   // we must allow threads already in critical regions to continue.
   144   //
   145   // JNI critical regions are the only participants in this scheme
   146   // because they are, by spec, well bounded while in a critical region.
   147   //
   148   // Each of the following two method is split into a fast path and a
   149   // slow path. JNICritical_lock is only grabbed in the slow path.
   150   // _needs_gc is initially false and every java thread will go
   151   // through the fast path, which simply increments or decrements the
   152   // current thread's critical count.  When GC happens at a safepoint,
   153   // GC_locker::is_active() is checked. Since there is no safepoint in
   154   // the fast path of lock_critical() and unlock_critical(), there is
   155   // no race condition between the fast path and GC. After _needs_gc
   156   // is set at a safepoint, every thread will go through the slow path
   157   // after the safepoint.  Since after a safepoint, each of the
   158   // following two methods is either entered from the method entry and
   159   // falls into the slow path, or is resumed from the safepoints in
   160   // the method, which only exist in the slow path. So when _needs_gc
   161   // is set, the slow path is always taken, till _needs_gc is cleared.
   162   static void lock_critical(JavaThread* thread);
   163   static void unlock_critical(JavaThread* thread);
   165   static address needs_gc_address() { return (address) &_needs_gc; }
   166 };
   169 // A No_GC_Verifier object can be placed in methods where one assumes that
   170 // no garbage collection will occur. The destructor will verify this property
   171 // unless the constructor is called with argument false (not verifygc).
   172 //
   173 // The check will only be done in debug mode and if verifygc true.
   175 class No_GC_Verifier: public StackObj {
   176  friend class Pause_No_GC_Verifier;
   178  protected:
   179   bool _verifygc;
   180   unsigned int _old_invocations;
   182  public:
   183 #ifdef ASSERT
   184   No_GC_Verifier(bool verifygc = true);
   185   ~No_GC_Verifier();
   186 #else
   187   No_GC_Verifier(bool verifygc = true) {}
   188   ~No_GC_Verifier() {}
   189 #endif
   190 };
   192 // A Pause_No_GC_Verifier is used to temporarily pause the behavior
   193 // of a No_GC_Verifier object. If we are not in debug mode or if the
   194 // No_GC_Verifier object has a _verifygc value of false, then there
   195 // is nothing to do.
   197 class Pause_No_GC_Verifier: public StackObj {
   198  private:
   199   No_GC_Verifier * _ngcv;
   201  public:
   202 #ifdef ASSERT
   203   Pause_No_GC_Verifier(No_GC_Verifier * ngcv);
   204   ~Pause_No_GC_Verifier();
   205 #else
   206   Pause_No_GC_Verifier(No_GC_Verifier * ngcv) {}
   207   ~Pause_No_GC_Verifier() {}
   208 #endif
   209 };
   212 // A No_Safepoint_Verifier object will throw an assertion failure if
   213 // the current thread passes a possible safepoint while this object is
   214 // instantiated. A safepoint, will either be: an oop allocation, blocking
   215 // on a Mutex or JavaLock, or executing a VM operation.
   216 //
   217 // If StrictSafepointChecks is turned off, it degrades into a No_GC_Verifier
   218 //
   219 class No_Safepoint_Verifier : public No_GC_Verifier {
   220  friend class Pause_No_Safepoint_Verifier;
   222  private:
   223   bool _activated;
   224   Thread *_thread;
   225  public:
   226 #ifdef ASSERT
   227   No_Safepoint_Verifier(bool activated = true, bool verifygc = true ) :
   228     No_GC_Verifier(verifygc),
   229     _activated(activated) {
   230     _thread = Thread::current();
   231     if (_activated) {
   232       _thread->_allow_allocation_count++;
   233       _thread->_allow_safepoint_count++;
   234     }
   235   }
   237   ~No_Safepoint_Verifier() {
   238     if (_activated) {
   239       _thread->_allow_allocation_count--;
   240       _thread->_allow_safepoint_count--;
   241     }
   242   }
   243 #else
   244   No_Safepoint_Verifier(bool activated = true, bool verifygc = true) : No_GC_Verifier(verifygc){}
   245   ~No_Safepoint_Verifier() {}
   246 #endif
   247 };
   249 // A Pause_No_Safepoint_Verifier is used to temporarily pause the
   250 // behavior of a No_Safepoint_Verifier object. If we are not in debug
   251 // mode then there is nothing to do. If the No_Safepoint_Verifier
   252 // object has an _activated value of false, then there is nothing to
   253 // do for safepoint and allocation checking, but there may still be
   254 // something to do for the underlying No_GC_Verifier object.
   256 class Pause_No_Safepoint_Verifier : public Pause_No_GC_Verifier {
   257  private:
   258   No_Safepoint_Verifier * _nsv;
   260  public:
   261 #ifdef ASSERT
   262   Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv)
   263     : Pause_No_GC_Verifier(nsv) {
   265     _nsv = nsv;
   266     if (_nsv->_activated) {
   267       _nsv->_thread->_allow_allocation_count--;
   268       _nsv->_thread->_allow_safepoint_count--;
   269     }
   270   }
   272   ~Pause_No_Safepoint_Verifier() {
   273     if (_nsv->_activated) {
   274       _nsv->_thread->_allow_allocation_count++;
   275       _nsv->_thread->_allow_safepoint_count++;
   276     }
   277   }
   278 #else
   279   Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv)
   280     : Pause_No_GC_Verifier(nsv) {}
   281   ~Pause_No_Safepoint_Verifier() {}
   282 #endif
   283 };
   285 // A SkipGCALot object is used to elide the usual effect of gc-a-lot
   286 // over a section of execution by a thread. Currently, it's used only to
   287 // prevent re-entrant calls to GC.
   288 class SkipGCALot : public StackObj {
   289   private:
   290    bool _saved;
   291    Thread* _t;
   293   public:
   294 #ifdef ASSERT
   295     SkipGCALot(Thread* t) : _t(t) {
   296       _saved = _t->skip_gcalot();
   297       _t->set_skip_gcalot(true);
   298     }
   300     ~SkipGCALot() {
   301       assert(_t->skip_gcalot(), "Save-restore protocol invariant");
   302       _t->set_skip_gcalot(_saved);
   303     }
   304 #else
   305     SkipGCALot(Thread* t) { }
   306     ~SkipGCALot() { }
   307 #endif
   308 };
   310 // JRT_LEAF currently can be called from either _thread_in_Java or
   311 // _thread_in_native mode. In _thread_in_native, it is ok
   312 // for another thread to trigger GC. The rest of the JRT_LEAF
   313 // rules apply.
   314 class JRT_Leaf_Verifier : public No_Safepoint_Verifier {
   315   static bool should_verify_GC();
   316  public:
   317 #ifdef ASSERT
   318   JRT_Leaf_Verifier();
   319   ~JRT_Leaf_Verifier();
   320 #else
   321   JRT_Leaf_Verifier() {}
   322   ~JRT_Leaf_Verifier() {}
   323 #endif
   324 };
   326 // A No_Alloc_Verifier object can be placed in methods where one assumes that
   327 // no allocation will occur. The destructor will verify this property
   328 // unless the constructor is called with argument false (not activated).
   329 //
   330 // The check will only be done in debug mode and if activated.
   331 // Note: this only makes sense at safepoints (otherwise, other threads may
   332 // allocate concurrently.)
   334 class No_Alloc_Verifier : public StackObj {
   335  private:
   336   bool  _activated;
   338  public:
   339 #ifdef ASSERT
   340   No_Alloc_Verifier(bool activated = true) {
   341     _activated = activated;
   342     if (_activated) Thread::current()->_allow_allocation_count++;
   343   }
   345   ~No_Alloc_Verifier() {
   346     if (_activated) Thread::current()->_allow_allocation_count--;
   347   }
   348 #else
   349   No_Alloc_Verifier(bool activated = true) {}
   350   ~No_Alloc_Verifier() {}
   351 #endif
   352 };
   354 #endif // SHARE_VM_MEMORY_GCLOCKER_HPP

mercurial