gcLocker.hpp revision 9727:f944761a3ce3
1/* 2 * Copyright (c) 1997, 2015, 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 */ 24 25#ifndef SHARE_VM_GC_SHARED_GCLOCKER_HPP 26#define SHARE_VM_GC_SHARED_GCLOCKER_HPP 27 28#include "gc/shared/collectedHeap.hpp" 29#include "gc/shared/genCollectedHeap.hpp" 30#include "memory/universe.hpp" 31#include "oops/oop.hpp" 32 33// The direct lock/unlock calls do not force a collection if an unlock 34// decrements the count to zero. Avoid calling these if at all possible. 35 36class GC_locker: public AllStatic { 37 private: 38 // The _jni_lock_count keeps track of the number of threads that are 39 // currently in a critical region. It's only kept up to date when 40 // _needs_gc is true. The current value is computed during 41 // safepointing and decremented during the slow path of GC_locker 42 // unlocking. 43 static volatile jint _jni_lock_count; // number of jni active instances. 44 static volatile bool _needs_gc; // heap is filling, we need a GC 45 // note: bool is typedef'd as jint 46 static volatile bool _doing_gc; // unlock_critical() is doing a GC 47 48#ifdef ASSERT 49 // This lock count is updated for all operations and is used to 50 // validate the jni_lock_count that is computed during safepoints. 51 static volatile jint _debug_jni_lock_count; 52#endif 53 54 // At a safepoint, visit all threads and count the number of active 55 // critical sections. This is used to ensure that all active 56 // critical sections are exited before a new one is started. 57 static void verify_critical_count() NOT_DEBUG_RETURN; 58 59 static void jni_lock(JavaThread* thread); 60 static void jni_unlock(JavaThread* thread); 61 62 static bool is_active_internal() { 63 verify_critical_count(); 64 return _jni_lock_count > 0; 65 } 66 67 static void log_debug_jni(const char* msg); 68 public: 69 // Accessors 70 static bool is_active() { 71 assert(SafepointSynchronize::is_at_safepoint(), "only read at safepoint"); 72 return is_active_internal(); 73 } 74 static bool needs_gc() { return _needs_gc; } 75 76 // Shorthand 77 static bool is_active_and_needs_gc() { 78 // Use is_active_internal since _needs_gc can change from true to 79 // false outside of a safepoint, triggering the assert in 80 // is_active. 81 return needs_gc() && is_active_internal(); 82 } 83 84 // In debug mode track the locking state at all times 85 static void increment_debug_jni_lock_count() NOT_DEBUG_RETURN; 86 static void decrement_debug_jni_lock_count() NOT_DEBUG_RETURN; 87 88 // Set the current lock count 89 static void set_jni_lock_count(int count) { 90 _jni_lock_count = count; 91 verify_critical_count(); 92 } 93 94 // Sets _needs_gc if is_active() is true. Returns is_active(). 95 static bool check_active_before_gc(); 96 97 // Stalls the caller (who should not be in a jni critical section) 98 // until needs_gc() clears. Note however that needs_gc() may be 99 // set at a subsequent safepoint and/or cleared under the 100 // JNICritical_lock, so the caller may not safely assert upon 101 // return from this method that "!needs_gc()" since that is 102 // not a stable predicate. 103 static void stall_until_clear(); 104 105 // The following two methods are used for JNI critical regions. 106 // If we find that we failed to perform a GC because the GC_locker 107 // was active, arrange for one as soon as possible by allowing 108 // all threads in critical regions to complete, but not allowing 109 // other critical regions to be entered. The reasons for that are: 110 // 1) a GC request won't be starved by overlapping JNI critical 111 // region activities, which can cause unnecessary OutOfMemory errors. 112 // 2) even if allocation requests can still be satisfied before GC locker 113 // becomes inactive, for example, in tenured generation possibly with 114 // heap expansion, those allocations can trigger lots of safepointing 115 // attempts (ineffective GC attempts) and require Heap_lock which 116 // slow down allocations tremendously. 117 // 118 // Note that critical regions can be nested in a single thread, so 119 // we must allow threads already in critical regions to continue. 120 // 121 // JNI critical regions are the only participants in this scheme 122 // because they are, by spec, well bounded while in a critical region. 123 // 124 // Each of the following two method is split into a fast path and a 125 // slow path. JNICritical_lock is only grabbed in the slow path. 126 // _needs_gc is initially false and every java thread will go 127 // through the fast path, which simply increments or decrements the 128 // current thread's critical count. When GC happens at a safepoint, 129 // GC_locker::is_active() is checked. Since there is no safepoint in 130 // the fast path of lock_critical() and unlock_critical(), there is 131 // no race condition between the fast path and GC. After _needs_gc 132 // is set at a safepoint, every thread will go through the slow path 133 // after the safepoint. Since after a safepoint, each of the 134 // following two methods is either entered from the method entry and 135 // falls into the slow path, or is resumed from the safepoints in 136 // the method, which only exist in the slow path. So when _needs_gc 137 // is set, the slow path is always taken, till _needs_gc is cleared. 138 static void lock_critical(JavaThread* thread); 139 static void unlock_critical(JavaThread* thread); 140 141 static address needs_gc_address() { return (address) &_needs_gc; } 142}; 143 144 145// A No_GC_Verifier object can be placed in methods where one assumes that 146// no garbage collection will occur. The destructor will verify this property 147// unless the constructor is called with argument false (not verifygc). 148// 149// The check will only be done in debug mode and if verifygc true. 150 151class No_GC_Verifier: public StackObj { 152 friend class Pause_No_GC_Verifier; 153 154 protected: 155 bool _verifygc; 156 unsigned int _old_invocations; 157 158 public: 159#ifdef ASSERT 160 No_GC_Verifier(bool verifygc = true); 161 ~No_GC_Verifier(); 162#else 163 No_GC_Verifier(bool verifygc = true) {} 164 ~No_GC_Verifier() {} 165#endif 166}; 167 168// A Pause_No_GC_Verifier is used to temporarily pause the behavior 169// of a No_GC_Verifier object. If we are not in debug mode or if the 170// No_GC_Verifier object has a _verifygc value of false, then there 171// is nothing to do. 172 173class Pause_No_GC_Verifier: public StackObj { 174 private: 175 No_GC_Verifier * _ngcv; 176 177 public: 178#ifdef ASSERT 179 Pause_No_GC_Verifier(No_GC_Verifier * ngcv); 180 ~Pause_No_GC_Verifier(); 181#else 182 Pause_No_GC_Verifier(No_GC_Verifier * ngcv) {} 183 ~Pause_No_GC_Verifier() {} 184#endif 185}; 186 187 188// A No_Safepoint_Verifier object will throw an assertion failure if 189// the current thread passes a possible safepoint while this object is 190// instantiated. A safepoint, will either be: an oop allocation, blocking 191// on a Mutex or JavaLock, or executing a VM operation. 192// 193// If StrictSafepointChecks is turned off, it degrades into a No_GC_Verifier 194// 195class No_Safepoint_Verifier : public No_GC_Verifier { 196 friend class Pause_No_Safepoint_Verifier; 197 198 private: 199 bool _activated; 200 Thread *_thread; 201 public: 202#ifdef ASSERT 203 No_Safepoint_Verifier(bool activated = true, bool verifygc = true ) : 204 No_GC_Verifier(verifygc), 205 _activated(activated) { 206 _thread = Thread::current(); 207 if (_activated) { 208 _thread->_allow_allocation_count++; 209 _thread->_allow_safepoint_count++; 210 } 211 } 212 213 ~No_Safepoint_Verifier() { 214 if (_activated) { 215 _thread->_allow_allocation_count--; 216 _thread->_allow_safepoint_count--; 217 } 218 } 219#else 220 No_Safepoint_Verifier(bool activated = true, bool verifygc = true) : No_GC_Verifier(verifygc){} 221 ~No_Safepoint_Verifier() {} 222#endif 223}; 224 225// A Pause_No_Safepoint_Verifier is used to temporarily pause the 226// behavior of a No_Safepoint_Verifier object. If we are not in debug 227// mode then there is nothing to do. If the No_Safepoint_Verifier 228// object has an _activated value of false, then there is nothing to 229// do for safepoint and allocation checking, but there may still be 230// something to do for the underlying No_GC_Verifier object. 231 232class Pause_No_Safepoint_Verifier : public Pause_No_GC_Verifier { 233 private: 234 No_Safepoint_Verifier * _nsv; 235 236 public: 237#ifdef ASSERT 238 Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv) 239 : Pause_No_GC_Verifier(nsv) { 240 241 _nsv = nsv; 242 if (_nsv->_activated) { 243 _nsv->_thread->_allow_allocation_count--; 244 _nsv->_thread->_allow_safepoint_count--; 245 } 246 } 247 248 ~Pause_No_Safepoint_Verifier() { 249 if (_nsv->_activated) { 250 _nsv->_thread->_allow_allocation_count++; 251 _nsv->_thread->_allow_safepoint_count++; 252 } 253 } 254#else 255 Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv) 256 : Pause_No_GC_Verifier(nsv) {} 257 ~Pause_No_Safepoint_Verifier() {} 258#endif 259}; 260 261// A SkipGCALot object is used to elide the usual effect of gc-a-lot 262// over a section of execution by a thread. Currently, it's used only to 263// prevent re-entrant calls to GC. 264class SkipGCALot : public StackObj { 265 private: 266 bool _saved; 267 Thread* _t; 268 269 public: 270#ifdef ASSERT 271 SkipGCALot(Thread* t) : _t(t) { 272 _saved = _t->skip_gcalot(); 273 _t->set_skip_gcalot(true); 274 } 275 276 ~SkipGCALot() { 277 assert(_t->skip_gcalot(), "Save-restore protocol invariant"); 278 _t->set_skip_gcalot(_saved); 279 } 280#else 281 SkipGCALot(Thread* t) { } 282 ~SkipGCALot() { } 283#endif 284}; 285 286// JRT_LEAF currently can be called from either _thread_in_Java or 287// _thread_in_native mode. In _thread_in_native, it is ok 288// for another thread to trigger GC. The rest of the JRT_LEAF 289// rules apply. 290class JRT_Leaf_Verifier : public No_Safepoint_Verifier { 291 static bool should_verify_GC(); 292 public: 293#ifdef ASSERT 294 JRT_Leaf_Verifier(); 295 ~JRT_Leaf_Verifier(); 296#else 297 JRT_Leaf_Verifier() {} 298 ~JRT_Leaf_Verifier() {} 299#endif 300}; 301 302// A No_Alloc_Verifier object can be placed in methods where one assumes that 303// no allocation will occur. The destructor will verify this property 304// unless the constructor is called with argument false (not activated). 305// 306// The check will only be done in debug mode and if activated. 307// Note: this only makes sense at safepoints (otherwise, other threads may 308// allocate concurrently.) 309 310class No_Alloc_Verifier : public StackObj { 311 private: 312 bool _activated; 313 314 public: 315#ifdef ASSERT 316 No_Alloc_Verifier(bool activated = true) { 317 _activated = activated; 318 if (_activated) Thread::current()->_allow_allocation_count++; 319 } 320 321 ~No_Alloc_Verifier() { 322 if (_activated) Thread::current()->_allow_allocation_count--; 323 } 324#else 325 No_Alloc_Verifier(bool activated = true) {} 326 ~No_Alloc_Verifier() {} 327#endif 328}; 329 330#endif // SHARE_VM_GC_SHARED_GCLOCKER_HPP 331