Mercurial > hg > graal-compiler
annotate src/share/vm/memory/gcLocker.cpp @ 4911:d903bf750e9f
7129514: time warp warnings after 7117303
Summary: Replace calls to os::javaTimeMillis() that are used to update the milliseconds since the last GC to an equivalent that uses a monotonically non-decreasing time source.
Reviewed-by: ysr, jmasa
author | johnc |
---|---|
date | Wed, 18 Jan 2012 09:50:16 -0800 |
parents | 0382d2b469b2 |
children | 09d00c18e323 |
rev | line source |
---|---|
0 | 1 /* |
4867
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
0 | 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 * | |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
196
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
196
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
196
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
1972 | 25 #include "precompiled.hpp" |
26 #include "memory/gcLocker.inline.hpp" | |
27 #include "memory/resourceArea.hpp" | |
28 #include "memory/sharedHeap.hpp" | |
0 | 29 |
30 volatile jint GC_locker::_jni_lock_count = 0; | |
31 volatile jint GC_locker::_lock_count = 0; | |
32 volatile bool GC_locker::_needs_gc = false; | |
33 volatile bool GC_locker::_doing_gc = false; | |
4867
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
34 jlong GC_locker::_wait_begin = 0; |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
35 |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
36 #ifdef ASSERT |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
37 volatile jint GC_locker::_debug_jni_lock_count = 0; |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
38 #endif |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
39 |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
40 |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
41 #ifdef ASSERT |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
42 void GC_locker::verify_critical_count() { |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
43 if (SafepointSynchronize::is_at_safepoint()) { |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
44 assert(!needs_gc() || _debug_jni_lock_count == _jni_lock_count, "must agree"); |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
45 int count = 0; |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
46 // Count the number of threads with critical operations in progress |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
47 for (JavaThread* thr = Threads::first(); thr; thr = thr->next()) { |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
48 if (thr->in_critical()) { |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
49 count++; |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
50 } |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
51 } |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
52 if (_jni_lock_count != count) { |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
53 tty->print_cr("critical counts don't match: %d != %d", _jni_lock_count, count); |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
54 for (JavaThread* thr = Threads::first(); thr; thr = thr->next()) { |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
55 if (thr->in_critical()) { |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
56 tty->print_cr(INTPTR_FORMAT " in_critical %d", thr, thr->in_critical()); |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
57 } |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
58 } |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
59 } |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
60 assert(_jni_lock_count == count, "must be equal"); |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
61 } |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
62 } |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
63 #endif |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
64 |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
65 bool GC_locker::check_active_before_gc() { |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
66 assert(SafepointSynchronize::is_at_safepoint(), "only read at safepoint"); |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
67 if (is_active() && !_needs_gc) { |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
68 verify_critical_count(); |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
69 _needs_gc = true; |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
70 if (PrintJNIGCStalls && PrintGCDetails) { |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
71 ResourceMark rm; // JavaThread::name() allocates to convert to UTF8 |
4873
0382d2b469b2
7013347: allow crypto functions to be called inline to enhance performance
never
parents:
4867
diff
changeset
|
72 _wait_begin = os::javaTimeNanos() / NANOSECS_PER_MILLISEC; |
4867
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
73 gclog_or_tty->print_cr(INT64_FORMAT ": Setting _needs_gc. Thread \"%s\" %d locked.", |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
74 _wait_begin, Thread::current()->name(), _jni_lock_count); |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
75 } |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
76 |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
77 } |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
78 return is_active(); |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
79 } |
0 | 80 |
81 void GC_locker::stall_until_clear() { | |
82 assert(!JavaThread::current()->in_critical(), "Would deadlock"); | |
4867
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
83 MutexLocker ml(JNICritical_lock); |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
84 |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
85 if (needs_gc()) { |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
86 if (PrintJNIGCStalls && PrintGCDetails) { |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
87 ResourceMark rm; // JavaThread::name() allocates to convert to UTF8 |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
88 gclog_or_tty->print_cr(INT64_FORMAT ": Allocation failed. Thread \"%s\" is stalled by JNI critical section, %d locked.", |
4873
0382d2b469b2
7013347: allow crypto functions to be called inline to enhance performance
never
parents:
4867
diff
changeset
|
89 (os::javaTimeNanos() / NANOSECS_PER_MILLISEC) - _wait_begin, Thread::current()->name(), _jni_lock_count); |
4867
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
90 } |
139
c0492d52d55b
6539517: CR 6186200 should be extended to perm gen allocation to prevent spurious OOM's from perm gen
apetrusenko
parents:
0
diff
changeset
|
91 } |
4867
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
92 |
0 | 93 // Wait for _needs_gc to be cleared |
4867
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
94 while (needs_gc()) { |
0 | 95 JNICritical_lock->wait(); |
96 } | |
97 } | |
98 | |
4867
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
99 void GC_locker::jni_lock(JavaThread* thread) { |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
100 assert(!thread->in_critical(), "shouldn't currently be in a critical region"); |
0 | 101 MutexLocker mu(JNICritical_lock); |
102 // Block entering threads if we know at least one thread is in a | |
103 // JNI critical region and we need a GC. | |
104 // We check that at least one thread is in a critical region before | |
105 // blocking because blocked threads are woken up by a thread exiting | |
106 // a JNI critical region. | |
4867
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
107 while ((needs_gc() && is_jni_active()) || _doing_gc) { |
0 | 108 JNICritical_lock->wait(); |
109 } | |
4867
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
110 thread->enter_critical(); |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
111 _jni_lock_count++; |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
112 increment_debug_jni_lock_count(); |
0 | 113 } |
114 | |
4867
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
115 void GC_locker::jni_unlock(JavaThread* thread) { |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
116 assert(thread->in_last_critical(), "should be exiting critical region"); |
0 | 117 MutexLocker mu(JNICritical_lock); |
4867
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
118 _jni_lock_count--; |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
119 decrement_debug_jni_lock_count(); |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
120 thread->exit_critical(); |
0 | 121 if (needs_gc() && !is_jni_active()) { |
122 // We're the last thread out. Cause a GC to occur. | |
123 // GC will also check is_active, so this check is not | |
124 // strictly needed. It's added here to make it clear that | |
125 // the GC will NOT be performed if any other caller | |
126 // of GC_locker::lock() still needs GC locked. | |
127 if (!is_active()) { | |
128 _doing_gc = true; | |
129 { | |
130 // Must give up the lock while at a safepoint | |
131 MutexUnlocker munlock(JNICritical_lock); | |
4867
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
132 if (PrintJNIGCStalls && PrintGCDetails) { |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
133 ResourceMark rm; // JavaThread::name() allocates to convert to UTF8 |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
134 gclog_or_tty->print_cr(INT64_FORMAT ": Thread \"%s\" is performing GC after exiting critical section, %d locked", |
4873
0382d2b469b2
7013347: allow crypto functions to be called inline to enhance performance
never
parents:
4867
diff
changeset
|
135 (os::javaTimeNanos() / NANOSECS_PER_MILLISEC) - _wait_begin, Thread::current()->name(), _jni_lock_count); |
4867
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
136 } |
0 | 137 Universe::heap()->collect(GCCause::_gc_locker); |
138 } | |
139 _doing_gc = false; | |
140 } | |
4867
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
141 |
1a2723f7ad8e
7129164: JNI Get/ReleasePrimitiveArrayCritical doesn't scale
never
parents:
1972
diff
changeset
|
142 _needs_gc = false; |
0 | 143 JNICritical_lock->notify_all(); |
144 } | |
145 } | |
146 | |
147 // Implementation of No_GC_Verifier | |
148 | |
149 #ifdef ASSERT | |
150 | |
151 No_GC_Verifier::No_GC_Verifier(bool verifygc) { | |
152 _verifygc = verifygc; | |
153 if (_verifygc) { | |
154 CollectedHeap* h = Universe::heap(); | |
155 assert(!h->is_gc_active(), "GC active during No_GC_Verifier"); | |
156 _old_invocations = h->total_collections(); | |
157 } | |
158 } | |
159 | |
160 | |
161 No_GC_Verifier::~No_GC_Verifier() { | |
162 if (_verifygc) { | |
163 CollectedHeap* h = Universe::heap(); | |
164 assert(!h->is_gc_active(), "GC active during No_GC_Verifier"); | |
165 if (_old_invocations != h->total_collections()) { | |
166 fatal("collection in a No_GC_Verifier secured function"); | |
167 } | |
168 } | |
169 } | |
170 | |
171 Pause_No_GC_Verifier::Pause_No_GC_Verifier(No_GC_Verifier * ngcv) { | |
172 _ngcv = ngcv; | |
173 if (_ngcv->_verifygc) { | |
174 // if we were verifying, then make sure that nothing is | |
175 // wrong before we "pause" verification | |
176 CollectedHeap* h = Universe::heap(); | |
177 assert(!h->is_gc_active(), "GC active during No_GC_Verifier"); | |
178 if (_ngcv->_old_invocations != h->total_collections()) { | |
179 fatal("collection in a No_GC_Verifier secured function"); | |
180 } | |
181 } | |
182 } | |
183 | |
184 | |
185 Pause_No_GC_Verifier::~Pause_No_GC_Verifier() { | |
186 if (_ngcv->_verifygc) { | |
187 // if we were verifying before, then reenable verification | |
188 CollectedHeap* h = Universe::heap(); | |
189 assert(!h->is_gc_active(), "GC active during No_GC_Verifier"); | |
190 _ngcv->_old_invocations = h->total_collections(); | |
191 } | |
192 } | |
193 | |
194 | |
195 // JRT_LEAF rules: | |
196 // A JRT_LEAF method may not interfere with safepointing by | |
197 // 1) acquiring or blocking on a Mutex or JavaLock - checked | |
198 // 2) allocating heap memory - checked | |
199 // 3) executing a VM operation - checked | |
200 // 4) executing a system call (including malloc) that could block or grab a lock | |
201 // 5) invoking GC | |
202 // 6) reaching a safepoint | |
203 // 7) running too long | |
204 // Nor may any method it calls. | |
205 JRT_Leaf_Verifier::JRT_Leaf_Verifier() | |
206 : No_Safepoint_Verifier(true, JRT_Leaf_Verifier::should_verify_GC()) | |
207 { | |
208 } | |
209 | |
210 JRT_Leaf_Verifier::~JRT_Leaf_Verifier() | |
211 { | |
212 } | |
213 | |
214 bool JRT_Leaf_Verifier::should_verify_GC() { | |
215 switch (JavaThread::current()->thread_state()) { | |
216 case _thread_in_Java: | |
217 // is in a leaf routine, there must be no safepoint. | |
218 return true; | |
219 case _thread_in_native: | |
220 // A native thread is not subject to safepoints. | |
221 // Even while it is in a leaf routine, GC is ok | |
222 return false; | |
223 default: | |
224 // Leaf routines cannot be called from other contexts. | |
225 ShouldNotReachHere(); | |
226 return false; | |
227 } | |
228 } | |
229 #endif |