annotate src/share/vm/memory/gcLocker.hpp @ 806:821269eca479

6820167: GCALotAtAllSafepoints + FullGCALot(ScavengeALot) options crash JVM Summary: Short-circuit gc-a-lot attempts by non-JavaThreads; SkipGCALot c'tor to elide re-entrant gc-a-lot attempts. Reviewed-by: apetrusenko, jcoomes, jmasa, kamg
author ysr
date Thu, 11 Jun 2009 12:40:00 -0700
parents d1605aabd0a1
children bd02caa94611
rev   line source
duke@0 1 /*
xdono@196 2 * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
duke@0 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 21 * have any questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 // The direct lock/unlock calls do not force a collection if an unlock
duke@0 26 // decrements the count to zero. Avoid calling these if at all possible.
duke@0 27
duke@0 28 class GC_locker: public AllStatic {
duke@0 29 private:
duke@0 30 static volatile jint _jni_lock_count; // number of jni active instances
duke@0 31 static volatile jint _lock_count; // number of other active instances
duke@0 32 static volatile bool _needs_gc; // heap is filling, we need a GC
duke@0 33 // note: bool is typedef'd as jint
duke@0 34 static volatile bool _doing_gc; // unlock_critical() is doing a GC
duke@0 35
duke@0 36 // Accessors
duke@0 37 static bool is_jni_active() {
duke@0 38 return _jni_lock_count > 0;
duke@0 39 }
duke@0 40
duke@0 41 static void set_needs_gc() {
duke@0 42 assert(SafepointSynchronize::is_at_safepoint(),
duke@0 43 "needs_gc is only set at a safepoint");
duke@0 44 _needs_gc = true;
duke@0 45 }
duke@0 46
duke@0 47 static void clear_needs_gc() {
duke@0 48 assert_lock_strong(JNICritical_lock);
duke@0 49 _needs_gc = false;
duke@0 50 }
duke@0 51
duke@0 52 static void jni_lock() {
duke@0 53 Atomic::inc(&_jni_lock_count);
duke@0 54 CHECK_UNHANDLED_OOPS_ONLY(
duke@0 55 if (CheckUnhandledOops) { Thread::current()->_gc_locked_out_count++; })
duke@0 56 assert(Universe::heap() == NULL || !Universe::heap()->is_gc_active(),
duke@0 57 "locking failed");
duke@0 58 }
duke@0 59
duke@0 60 static void jni_unlock() {
duke@0 61 Atomic::dec(&_jni_lock_count);
duke@0 62 CHECK_UNHANDLED_OOPS_ONLY(
duke@0 63 if (CheckUnhandledOops) { Thread::current()->_gc_locked_out_count--; })
duke@0 64 }
duke@0 65
duke@0 66 static void jni_lock_slow();
duke@0 67 static void jni_unlock_slow();
duke@0 68
duke@0 69 public:
duke@0 70 // Accessors
duke@0 71 static bool is_active();
duke@0 72 static bool needs_gc() { return _needs_gc; }
duke@0 73 // Shorthand
duke@0 74 static bool is_active_and_needs_gc() { return is_active() && needs_gc();}
duke@0 75
duke@0 76 // Calls set_needs_gc() if is_active() is true. Returns is_active().
duke@0 77 static bool check_active_before_gc();
duke@0 78
duke@0 79 // Stalls the caller (who should not be in a jni critical section)
duke@0 80 // until needs_gc() clears. Note however that needs_gc() may be
duke@0 81 // set at a subsequent safepoint and/or cleared under the
duke@0 82 // JNICritical_lock, so the caller may not safely assert upon
duke@0 83 // return from this method that "!needs_gc()" since that is
duke@0 84 // not a stable predicate.
duke@0 85 static void stall_until_clear();
duke@0 86
duke@0 87 // Non-structured GC locking: currently needed for JNI. Use with care!
duke@0 88 static void lock();
duke@0 89 static void unlock();
duke@0 90
duke@0 91 // The following two methods are used for JNI critical regions.
duke@0 92 // If we find that we failed to perform a GC because the GC_locker
duke@0 93 // was active, arrange for one as soon as possible by allowing
duke@0 94 // all threads in critical regions to complete, but not allowing
duke@0 95 // other critical regions to be entered. The reasons for that are:
duke@0 96 // 1) a GC request won't be starved by overlapping JNI critical
duke@0 97 // region activities, which can cause unnecessary OutOfMemory errors.
duke@0 98 // 2) even if allocation requests can still be satisfied before GC locker
duke@0 99 // becomes inactive, for example, in tenured generation possibly with
duke@0 100 // heap expansion, those allocations can trigger lots of safepointing
duke@0 101 // attempts (ineffective GC attempts) and require Heap_lock which
duke@0 102 // slow down allocations tremendously.
duke@0 103 //
duke@0 104 // Note that critical regions can be nested in a single thread, so
duke@0 105 // we must allow threads already in critical regions to continue.
duke@0 106 //
duke@0 107 // JNI critical regions are the only participants in this scheme
duke@0 108 // because they are, by spec, well bounded while in a critical region.
duke@0 109 //
duke@0 110 // Each of the following two method is split into a fast path and a slow
duke@0 111 // path. JNICritical_lock is only grabbed in the slow path.
duke@0 112 // _needs_gc is initially false and every java thread will go
duke@0 113 // through the fast path (which does the same thing as the slow path
duke@0 114 // when _needs_gc is false). When GC happens at a safepoint,
duke@0 115 // GC_locker::is_active() is checked. Since there is no safepoint in the
duke@0 116 // fast path of lock_critical() and unlock_critical(), there is no race
duke@0 117 // condition between the fast path and GC. After _needs_gc is set at a
duke@0 118 // safepoint, every thread will go through the slow path after the safepoint.
duke@0 119 // Since after a safepoint, each of the following two methods is either
duke@0 120 // entered from the method entry and falls into the slow path, or is
duke@0 121 // resumed from the safepoints in the method, which only exist in the slow
duke@0 122 // path. So when _needs_gc is set, the slow path is always taken, till
duke@0 123 // _needs_gc is cleared.
duke@0 124 static void lock_critical(JavaThread* thread);
duke@0 125 static void unlock_critical(JavaThread* thread);
duke@0 126 };
duke@0 127
duke@0 128
duke@0 129 // A No_GC_Verifier object can be placed in methods where one assumes that
duke@0 130 // no garbage collection will occur. The destructor will verify this property
duke@0 131 // unless the constructor is called with argument false (not verifygc).
duke@0 132 //
duke@0 133 // The check will only be done in debug mode and if verifygc true.
duke@0 134
duke@0 135 class No_GC_Verifier: public StackObj {
duke@0 136 friend class Pause_No_GC_Verifier;
duke@0 137
duke@0 138 protected:
duke@0 139 bool _verifygc;
duke@0 140 unsigned int _old_invocations;
duke@0 141
duke@0 142 public:
duke@0 143 #ifdef ASSERT
duke@0 144 No_GC_Verifier(bool verifygc = true);
duke@0 145 ~No_GC_Verifier();
duke@0 146 #else
duke@0 147 No_GC_Verifier(bool verifygc = true) {}
duke@0 148 ~No_GC_Verifier() {}
duke@0 149 #endif
duke@0 150 };
duke@0 151
duke@0 152 // A Pause_No_GC_Verifier is used to temporarily pause the behavior
duke@0 153 // of a No_GC_Verifier object. If we are not in debug mode or if the
duke@0 154 // No_GC_Verifier object has a _verifygc value of false, then there
duke@0 155 // is nothing to do.
duke@0 156
duke@0 157 class Pause_No_GC_Verifier: public StackObj {
duke@0 158 private:
duke@0 159 No_GC_Verifier * _ngcv;
duke@0 160
duke@0 161 public:
duke@0 162 #ifdef ASSERT
duke@0 163 Pause_No_GC_Verifier(No_GC_Verifier * ngcv);
duke@0 164 ~Pause_No_GC_Verifier();
duke@0 165 #else
duke@0 166 Pause_No_GC_Verifier(No_GC_Verifier * ngcv) {}
duke@0 167 ~Pause_No_GC_Verifier() {}
duke@0 168 #endif
duke@0 169 };
duke@0 170
duke@0 171
duke@0 172 // A No_Safepoint_Verifier object will throw an assertion failure if
duke@0 173 // the current thread passes a possible safepoint while this object is
duke@0 174 // instantiated. A safepoint, will either be: an oop allocation, blocking
duke@0 175 // on a Mutex or JavaLock, or executing a VM operation.
duke@0 176 //
duke@0 177 // If StrictSafepointChecks is turned off, it degrades into a No_GC_Verifier
duke@0 178 //
duke@0 179 class No_Safepoint_Verifier : public No_GC_Verifier {
duke@0 180 friend class Pause_No_Safepoint_Verifier;
duke@0 181
duke@0 182 private:
duke@0 183 bool _activated;
duke@0 184 Thread *_thread;
duke@0 185 public:
duke@0 186 #ifdef ASSERT
never@98 187 No_Safepoint_Verifier(bool activated = true, bool verifygc = true ) :
never@98 188 No_GC_Verifier(verifygc),
never@98 189 _activated(activated) {
duke@0 190 _thread = Thread::current();
duke@0 191 if (_activated) {
duke@0 192 _thread->_allow_allocation_count++;
duke@0 193 _thread->_allow_safepoint_count++;
duke@0 194 }
duke@0 195 }
duke@0 196
duke@0 197 ~No_Safepoint_Verifier() {
duke@0 198 if (_activated) {
duke@0 199 _thread->_allow_allocation_count--;
duke@0 200 _thread->_allow_safepoint_count--;
duke@0 201 }
duke@0 202 }
duke@0 203 #else
duke@0 204 No_Safepoint_Verifier(bool activated = true, bool verifygc = true) : No_GC_Verifier(verifygc){}
duke@0 205 ~No_Safepoint_Verifier() {}
duke@0 206 #endif
duke@0 207 };
duke@0 208
duke@0 209 // A Pause_No_Safepoint_Verifier is used to temporarily pause the
duke@0 210 // behavior of a No_Safepoint_Verifier object. If we are not in debug
duke@0 211 // mode then there is nothing to do. If the No_Safepoint_Verifier
duke@0 212 // object has an _activated value of false, then there is nothing to
duke@0 213 // do for safepoint and allocation checking, but there may still be
duke@0 214 // something to do for the underlying No_GC_Verifier object.
duke@0 215
duke@0 216 class Pause_No_Safepoint_Verifier : public Pause_No_GC_Verifier {
duke@0 217 private:
duke@0 218 No_Safepoint_Verifier * _nsv;
duke@0 219
duke@0 220 public:
duke@0 221 #ifdef ASSERT
duke@0 222 Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv)
duke@0 223 : Pause_No_GC_Verifier(nsv) {
duke@0 224
duke@0 225 _nsv = nsv;
duke@0 226 if (_nsv->_activated) {
duke@0 227 _nsv->_thread->_allow_allocation_count--;
duke@0 228 _nsv->_thread->_allow_safepoint_count--;
duke@0 229 }
duke@0 230 }
duke@0 231
duke@0 232 ~Pause_No_Safepoint_Verifier() {
duke@0 233 if (_nsv->_activated) {
duke@0 234 _nsv->_thread->_allow_allocation_count++;
duke@0 235 _nsv->_thread->_allow_safepoint_count++;
duke@0 236 }
duke@0 237 }
duke@0 238 #else
duke@0 239 Pause_No_Safepoint_Verifier(No_Safepoint_Verifier * nsv)
duke@0 240 : Pause_No_GC_Verifier(nsv) {}
duke@0 241 ~Pause_No_Safepoint_Verifier() {}
duke@0 242 #endif
duke@0 243 };
duke@0 244
ysr@806 245 // A SkipGCALot object is used to elide the usual effect of gc-a-lot
ysr@806 246 // over a section of execution by a thread. Currently, it's used only to
ysr@806 247 // prevent re-entrant calls to GC.
ysr@806 248 class SkipGCALot : public StackObj {
ysr@806 249 private:
ysr@806 250 bool _saved;
ysr@806 251 Thread* _t;
ysr@806 252
ysr@806 253 public:
ysr@806 254 #ifdef ASSERT
ysr@806 255 SkipGCALot(Thread* t) : _t(t) {
ysr@806 256 _saved = _t->skip_gcalot();
ysr@806 257 _t->set_skip_gcalot(true);
ysr@806 258 }
ysr@806 259
ysr@806 260 ~SkipGCALot() {
ysr@806 261 assert(_t->skip_gcalot(), "Save-restore protocol invariant");
ysr@806 262 _t->set_skip_gcalot(_saved);
ysr@806 263 }
ysr@806 264 #else
ysr@806 265 SkipGCALot(Thread* t) { }
ysr@806 266 ~SkipGCALot() { }
ysr@806 267 #endif
ysr@806 268 };
ysr@806 269
duke@0 270 // JRT_LEAF currently can be called from either _thread_in_Java or
duke@0 271 // _thread_in_native mode. In _thread_in_native, it is ok
duke@0 272 // for another thread to trigger GC. The rest of the JRT_LEAF
duke@0 273 // rules apply.
duke@0 274 class JRT_Leaf_Verifier : public No_Safepoint_Verifier {
duke@0 275 static bool should_verify_GC();
duke@0 276 public:
duke@0 277 #ifdef ASSERT
duke@0 278 JRT_Leaf_Verifier();
duke@0 279 ~JRT_Leaf_Verifier();
duke@0 280 #else
duke@0 281 JRT_Leaf_Verifier() {}
duke@0 282 ~JRT_Leaf_Verifier() {}
duke@0 283 #endif
duke@0 284 };
duke@0 285
duke@0 286 // A No_Alloc_Verifier object can be placed in methods where one assumes that
duke@0 287 // no allocation will occur. The destructor will verify this property
duke@0 288 // unless the constructor is called with argument false (not activated).
duke@0 289 //
duke@0 290 // The check will only be done in debug mode and if activated.
duke@0 291 // Note: this only makes sense at safepoints (otherwise, other threads may
duke@0 292 // allocate concurrently.)
duke@0 293
duke@0 294 class No_Alloc_Verifier : public StackObj {
duke@0 295 private:
duke@0 296 bool _activated;
duke@0 297
duke@0 298 public:
duke@0 299 #ifdef ASSERT
duke@0 300 No_Alloc_Verifier(bool activated = true) {
duke@0 301 _activated = activated;
duke@0 302 if (_activated) Thread::current()->_allow_allocation_count++;
duke@0 303 }
duke@0 304
duke@0 305 ~No_Alloc_Verifier() {
duke@0 306 if (_activated) Thread::current()->_allow_allocation_count--;
duke@0 307 }
duke@0 308 #else
duke@0 309 No_Alloc_Verifier(bool activated = true) {}
duke@0 310 ~No_Alloc_Verifier() {}
duke@0 311 #endif
duke@0 312 };