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