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