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