src/share/vm/memory/gcLocker.hpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/memory/gcLocker.hpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,352 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#ifndef SHARE_VM_MEMORY_GCLOCKER_HPP
    1.29 +#define SHARE_VM_MEMORY_GCLOCKER_HPP
    1.30 +
    1.31 +#include "gc_interface/collectedHeap.hpp"
    1.32 +#include "memory/genCollectedHeap.hpp"
    1.33 +#include "memory/universe.hpp"
    1.34 +#include "oops/oop.hpp"
    1.35 +#include "runtime/thread.inline.hpp"
    1.36 +#ifdef TARGET_OS_FAMILY_linux
    1.37 +# include "os_linux.inline.hpp"
    1.38 +#endif
    1.39 +#ifdef TARGET_OS_FAMILY_solaris
    1.40 +# include "os_solaris.inline.hpp"
    1.41 +#endif
    1.42 +#ifdef TARGET_OS_FAMILY_windows
    1.43 +# include "os_windows.inline.hpp"
    1.44 +#endif
    1.45 +#ifdef TARGET_OS_FAMILY_bsd
    1.46 +# include "os_bsd.inline.hpp"
    1.47 +#endif
    1.48 +
    1.49 +// The direct lock/unlock calls do not force a collection if an unlock
    1.50 +// decrements the count to zero. Avoid calling these if at all possible.
    1.51 +
    1.52 +class GC_locker: public AllStatic {
    1.53 + private:
    1.54 +  // The _jni_lock_count keeps track of the number of threads that are
    1.55 +  // currently in a critical region.  It's only kept up to date when
    1.56 +  // _needs_gc is true.  The current value is computed during
    1.57 +  // safepointing and decremented during the slow path of GC_locker
    1.58 +  // unlocking.
    1.59 +  static volatile jint _jni_lock_count;  // number of jni active instances.
    1.60 +  static volatile bool _needs_gc;        // heap is filling, we need a GC
    1.61 +                                         // note: bool is typedef'd as jint
    1.62 +  static volatile bool _doing_gc;        // unlock_critical() is doing a GC
    1.63 +
    1.64 +#ifdef ASSERT
    1.65 +  // This lock count is updated for all operations and is used to
    1.66 +  // validate the jni_lock_count that is computed during safepoints.
    1.67 +  static volatile jint _debug_jni_lock_count;
    1.68 +#endif
    1.69 +
    1.70 +  // At a safepoint, visit all threads and count the number of active
    1.71 +  // critical sections.  This is used to ensure that all active
    1.72 +  // critical sections are exited before a new one is started.
    1.73 +  static void verify_critical_count() NOT_DEBUG_RETURN;
    1.74 +
    1.75 +  static void jni_lock(JavaThread* thread);
    1.76 +  static void jni_unlock(JavaThread* thread);
    1.77 +
    1.78 +  static bool is_active_internal() {
    1.79 +    verify_critical_count();
    1.80 +    return _jni_lock_count > 0;
    1.81 +  }
    1.82 +
    1.83 + public:
    1.84 +  // Accessors
    1.85 +  static bool is_active() {
    1.86 +    assert(SafepointSynchronize::is_at_safepoint(), "only read at safepoint");
    1.87 +    return is_active_internal();
    1.88 +  }
    1.89 +  static bool needs_gc()       { return _needs_gc;                        }
    1.90 +
    1.91 +  // Shorthand
    1.92 +  static bool is_active_and_needs_gc() {
    1.93 +    // Use is_active_internal since _needs_gc can change from true to
    1.94 +    // false outside of a safepoint, triggering the assert in
    1.95 +    // is_active.
    1.96 +    return needs_gc() && is_active_internal();
    1.97 +  }
    1.98 +
    1.99 +  // In debug mode track the locking state at all times
   1.100 +  static void increment_debug_jni_lock_count() {
   1.101 +#ifdef ASSERT
   1.102 +    assert(_debug_jni_lock_count >= 0, "bad value");
   1.103 +    Atomic::inc(&_debug_jni_lock_count);
   1.104 +#endif
   1.105 +  }
   1.106 +  static void decrement_debug_jni_lock_count() {
   1.107 +#ifdef ASSERT
   1.108 +    assert(_debug_jni_lock_count > 0, "bad value");
   1.109 +    Atomic::dec(&_debug_jni_lock_count);
   1.110 +#endif
   1.111 +  }
   1.112 +
   1.113 +  // Set the current lock count
   1.114 +  static void set_jni_lock_count(int count) {
   1.115 +    _jni_lock_count = count;
   1.116 +    verify_critical_count();
   1.117 +  }
   1.118 +
   1.119 +  // Sets _needs_gc if is_active() is true. Returns is_active().
   1.120 +  static bool check_active_before_gc();
   1.121 +
   1.122 +  // Stalls the caller (who should not be in a jni critical section)
   1.123 +  // until needs_gc() clears. Note however that needs_gc() may be
   1.124 +  // set at a subsequent safepoint and/or cleared under the
   1.125 +  // JNICritical_lock, so the caller may not safely assert upon
   1.126 +  // return from this method that "!needs_gc()" since that is
   1.127 +  // not a stable predicate.
   1.128 +  static void stall_until_clear();
   1.129 +
   1.130 +  // The following two methods are used for JNI critical regions.
   1.131 +  // If we find that we failed to perform a GC because the GC_locker
   1.132 +  // was active, arrange for one as soon as possible by allowing
   1.133 +  // all threads in critical regions to complete, but not allowing
   1.134 +  // other critical regions to be entered. The reasons for that are:
   1.135 +  // 1) a GC request won't be starved by overlapping JNI critical
   1.136 +  //    region activities, which can cause unnecessary OutOfMemory errors.
   1.137 +  // 2) even if allocation requests can still be satisfied before GC locker
   1.138 +  //    becomes inactive, for example, in tenured generation possibly with
   1.139 +  //    heap expansion, those allocations can trigger lots of safepointing
   1.140 +  //    attempts (ineffective GC attempts) and require Heap_lock which
   1.141 +  //    slow down allocations tremendously.
   1.142 +  //
   1.143 +  // Note that critical regions can be nested in a single thread, so
   1.144 +  // we must allow threads already in critical regions to continue.
   1.145 +  //
   1.146 +  // JNI critical regions are the only participants in this scheme
   1.147 +  // because they are, by spec, well bounded while in a critical region.
   1.148 +  //
   1.149 +  // Each of the following two method is split into a fast path and a
   1.150 +  // slow path. JNICritical_lock is only grabbed in the slow path.
   1.151 +  // _needs_gc is initially false and every java thread will go
   1.152 +  // through the fast path, which simply increments or decrements the
   1.153 +  // current thread's critical count.  When GC happens at a safepoint,
   1.154 +  // GC_locker::is_active() is checked. Since there is no safepoint in
   1.155 +  // the fast path of lock_critical() and unlock_critical(), there is
   1.156 +  // no race condition between the fast path and GC. After _needs_gc
   1.157 +  // is set at a safepoint, every thread will go through the slow path
   1.158 +  // after the safepoint.  Since after a safepoint, each of the
   1.159 +  // following two methods is either entered from the method entry and
   1.160 +  // falls into the slow path, or is resumed from the safepoints in
   1.161 +  // the method, which only exist in the slow path. So when _needs_gc
   1.162 +  // is set, the slow path is always taken, till _needs_gc is cleared.
   1.163 +  static void lock_critical(JavaThread* thread);
   1.164 +  static void unlock_critical(JavaThread* thread);
   1.165 +
   1.166 +  static address needs_gc_address() { return (address) &_needs_gc; }
   1.167 +};
   1.168 +
   1.169 +
   1.170 +// A No_GC_Verifier object can be placed in methods where one assumes that
   1.171 +// no garbage collection will occur. The destructor will verify this property
   1.172 +// unless the constructor is called with argument false (not verifygc).
   1.173 +//
   1.174 +// The check will only be done in debug mode and if verifygc true.
   1.175 +
   1.176 +class No_GC_Verifier: public StackObj {
   1.177 + friend class Pause_No_GC_Verifier;
   1.178 +
   1.179 + protected:
   1.180 +  bool _verifygc;
   1.181 +  unsigned int _old_invocations;
   1.182 +
   1.183 + public:
   1.184 +#ifdef ASSERT
   1.185 +  No_GC_Verifier(bool verifygc = true);
   1.186 +  ~No_GC_Verifier();
   1.187 +#else
   1.188 +  No_GC_Verifier(bool verifygc = true) {}
   1.189 +  ~No_GC_Verifier() {}
   1.190 +#endif
   1.191 +};
   1.192 +
   1.193 +// A Pause_No_GC_Verifier is used to temporarily pause the behavior
   1.194 +// of a No_GC_Verifier object. If we are not in debug mode or if the
   1.195 +// No_GC_Verifier object has a _verifygc value of false, then there
   1.196 +// is nothing to do.
   1.197 +
   1.198 +class Pause_No_GC_Verifier: public StackObj {
   1.199 + private:
   1.200 +  No_GC_Verifier * _ngcv;
   1.201 +
   1.202 + public:
   1.203 +#ifdef ASSERT
   1.204 +  Pause_No_GC_Verifier(No_GC_Verifier * ngcv);
   1.205 +  ~Pause_No_GC_Verifier();
   1.206 +#else
   1.207 +  Pause_No_GC_Verifier(No_GC_Verifier * ngcv) {}
   1.208 +  ~Pause_No_GC_Verifier() {}
   1.209 +#endif
   1.210 +};
   1.211 +
   1.212 +
   1.213 +// A No_Safepoint_Verifier object will throw an assertion failure if
   1.214 +// the current thread passes a possible safepoint while this object is
   1.215 +// instantiated. A safepoint, will either be: an oop allocation, blocking
   1.216 +// on a Mutex or JavaLock, or executing a VM operation.
   1.217 +//
   1.218 +// If StrictSafepointChecks is turned off, it degrades into a No_GC_Verifier
   1.219 +//
   1.220 +class No_Safepoint_Verifier : public No_GC_Verifier {
   1.221 + friend class Pause_No_Safepoint_Verifier;
   1.222 +
   1.223 + private:
   1.224 +  bool _activated;
   1.225 +  Thread *_thread;
   1.226 + public:
   1.227 +#ifdef ASSERT
   1.228 +  No_Safepoint_Verifier(bool activated = true, bool verifygc = true ) :
   1.229 +    No_GC_Verifier(verifygc),
   1.230 +    _activated(activated) {
   1.231 +    _thread = Thread::current();
   1.232 +    if (_activated) {
   1.233 +      _thread->_allow_allocation_count++;
   1.234 +      _thread->_allow_safepoint_count++;
   1.235 +    }
   1.236 +  }
   1.237 +
   1.238 +  ~No_Safepoint_Verifier() {
   1.239 +    if (_activated) {
   1.240 +      _thread->_allow_allocation_count--;
   1.241 +      _thread->_allow_safepoint_count--;
   1.242 +    }
   1.243 +  }
   1.244 +#else
   1.245 +  No_Safepoint_Verifier(bool activated = true, bool verifygc = true) : No_GC_Verifier(verifygc){}
   1.246 +  ~No_Safepoint_Verifier() {}
   1.247 +#endif
   1.248 +};
   1.249 +
   1.250 +// A Pause_No_Safepoint_Verifier is used to temporarily pause the
   1.251 +// behavior of a No_Safepoint_Verifier object. If we are not in debug
   1.252 +// mode then there is nothing to do. If the No_Safepoint_Verifier
   1.253 +// object has an _activated value of false, then there is nothing to
   1.254 +// do for safepoint and allocation checking, but there may still be
   1.255 +// something to do for the underlying No_GC_Verifier object.
   1.256 +
   1.257 +class Pause_No_Safepoint_Verifier : public Pause_No_GC_Verifier {
   1.258 + private:
   1.259 +  No_Safepoint_Verifier * _nsv;
   1.260 +
   1.261 + public:
   1.262 +#ifdef ASSERT
   1.263 +  Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv)
   1.264 +    : Pause_No_GC_Verifier(nsv) {
   1.265 +
   1.266 +    _nsv = nsv;
   1.267 +    if (_nsv->_activated) {
   1.268 +      _nsv->_thread->_allow_allocation_count--;
   1.269 +      _nsv->_thread->_allow_safepoint_count--;
   1.270 +    }
   1.271 +  }
   1.272 +
   1.273 +  ~Pause_No_Safepoint_Verifier() {
   1.274 +    if (_nsv->_activated) {
   1.275 +      _nsv->_thread->_allow_allocation_count++;
   1.276 +      _nsv->_thread->_allow_safepoint_count++;
   1.277 +    }
   1.278 +  }
   1.279 +#else
   1.280 +  Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv)
   1.281 +    : Pause_No_GC_Verifier(nsv) {}
   1.282 +  ~Pause_No_Safepoint_Verifier() {}
   1.283 +#endif
   1.284 +};
   1.285 +
   1.286 +// A SkipGCALot object is used to elide the usual effect of gc-a-lot
   1.287 +// over a section of execution by a thread. Currently, it's used only to
   1.288 +// prevent re-entrant calls to GC.
   1.289 +class SkipGCALot : public StackObj {
   1.290 +  private:
   1.291 +   bool _saved;
   1.292 +   Thread* _t;
   1.293 +
   1.294 +  public:
   1.295 +#ifdef ASSERT
   1.296 +    SkipGCALot(Thread* t) : _t(t) {
   1.297 +      _saved = _t->skip_gcalot();
   1.298 +      _t->set_skip_gcalot(true);
   1.299 +    }
   1.300 +
   1.301 +    ~SkipGCALot() {
   1.302 +      assert(_t->skip_gcalot(), "Save-restore protocol invariant");
   1.303 +      _t->set_skip_gcalot(_saved);
   1.304 +    }
   1.305 +#else
   1.306 +    SkipGCALot(Thread* t) { }
   1.307 +    ~SkipGCALot() { }
   1.308 +#endif
   1.309 +};
   1.310 +
   1.311 +// JRT_LEAF currently can be called from either _thread_in_Java or
   1.312 +// _thread_in_native mode. In _thread_in_native, it is ok
   1.313 +// for another thread to trigger GC. The rest of the JRT_LEAF
   1.314 +// rules apply.
   1.315 +class JRT_Leaf_Verifier : public No_Safepoint_Verifier {
   1.316 +  static bool should_verify_GC();
   1.317 + public:
   1.318 +#ifdef ASSERT
   1.319 +  JRT_Leaf_Verifier();
   1.320 +  ~JRT_Leaf_Verifier();
   1.321 +#else
   1.322 +  JRT_Leaf_Verifier() {}
   1.323 +  ~JRT_Leaf_Verifier() {}
   1.324 +#endif
   1.325 +};
   1.326 +
   1.327 +// A No_Alloc_Verifier object can be placed in methods where one assumes that
   1.328 +// no allocation will occur. The destructor will verify this property
   1.329 +// unless the constructor is called with argument false (not activated).
   1.330 +//
   1.331 +// The check will only be done in debug mode and if activated.
   1.332 +// Note: this only makes sense at safepoints (otherwise, other threads may
   1.333 +// allocate concurrently.)
   1.334 +
   1.335 +class No_Alloc_Verifier : public StackObj {
   1.336 + private:
   1.337 +  bool  _activated;
   1.338 +
   1.339 + public:
   1.340 +#ifdef ASSERT
   1.341 +  No_Alloc_Verifier(bool activated = true) {
   1.342 +    _activated = activated;
   1.343 +    if (_activated) Thread::current()->_allow_allocation_count++;
   1.344 +  }
   1.345 +
   1.346 +  ~No_Alloc_Verifier() {
   1.347 +    if (_activated) Thread::current()->_allow_allocation_count--;
   1.348 +  }
   1.349 +#else
   1.350 +  No_Alloc_Verifier(bool activated = true) {}
   1.351 +  ~No_Alloc_Verifier() {}
   1.352 +#endif
   1.353 +};
   1.354 +
   1.355 +#endif // SHARE_VM_MEMORY_GCLOCKER_HPP

mercurial