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