author | stefank |
Tue, 15 Mar 2016 10:11:02 +0100 | |
changeset 37106 | 0856b64af754 |
parent 36398 | 5983833c444a |
child 37179 | 4dbcb3a642d2 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
36398
5983833c444a
8146849: Remove TraceJNIHandleAllocation rather than converting to UL
gziemski
parents:
35061
diff
changeset
|
2 |
* Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved. |
1 | 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 |
* |
|
5547
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
4571
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
4571
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
4571
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
||
7397 | 25 |
#include "precompiled.hpp" |
26 |
#include "classfile/systemDictionary.hpp" |
|
35061 | 27 |
#include "logging/log.hpp" |
37106 | 28 |
#include "memory/iterator.hpp" |
7397 | 29 |
#include "oops/oop.inline.hpp" |
7918
ce1e4ae77aea
7012505: BreakpointWithFullGC.sh fails with Internal Error (src/share/vm/oops/methodOop.cpp:220)
kamg
parents:
7896
diff
changeset
|
30 |
#include "prims/jvmtiExport.hpp" |
7397 | 31 |
#include "runtime/jniHandles.hpp" |
32 |
#include "runtime/mutexLocker.hpp" |
|
14583
d70ee55535f4
8003935: Simplify the needed includes for using Thread::current()
stefank
parents:
13728
diff
changeset
|
33 |
#include "runtime/thread.inline.hpp" |
1 | 34 |
|
35 |
JNIHandleBlock* JNIHandles::_global_handles = NULL; |
|
36 |
JNIHandleBlock* JNIHandles::_weak_global_handles = NULL; |
|
37 |
oop JNIHandles::_deleted_handle = NULL; |
|
38 |
||
39 |
||
40 |
jobject JNIHandles::make_local(oop obj) { |
|
41 |
if (obj == NULL) { |
|
42 |
return NULL; // ignore null handles |
|
43 |
} else { |
|
44 |
Thread* thread = Thread::current(); |
|
45 |
assert(Universe::heap()->is_in_reserved(obj), "sanity check"); |
|
46 |
return thread->active_handles()->allocate_handle(obj); |
|
47 |
} |
|
48 |
} |
|
49 |
||
50 |
||
51 |
// optimized versions |
|
52 |
||
53 |
jobject JNIHandles::make_local(Thread* thread, oop obj) { |
|
54 |
if (obj == NULL) { |
|
55 |
return NULL; // ignore null handles |
|
56 |
} else { |
|
57 |
assert(Universe::heap()->is_in_reserved(obj), "sanity check"); |
|
58 |
return thread->active_handles()->allocate_handle(obj); |
|
59 |
} |
|
60 |
} |
|
61 |
||
62 |
||
63 |
jobject JNIHandles::make_local(JNIEnv* env, oop obj) { |
|
64 |
if (obj == NULL) { |
|
65 |
return NULL; // ignore null handles |
|
66 |
} else { |
|
67 |
JavaThread* thread = JavaThread::thread_from_jni_environment(env); |
|
68 |
assert(Universe::heap()->is_in_reserved(obj), "sanity check"); |
|
69 |
return thread->active_handles()->allocate_handle(obj); |
|
70 |
} |
|
71 |
} |
|
72 |
||
73 |
||
74 |
jobject JNIHandles::make_global(Handle obj) { |
|
5896
c60924a89622
6656830: assert((*p)->is_oop(),"expected an oop while scanning weak refs")
never
parents:
5547
diff
changeset
|
75 |
assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); |
1 | 76 |
jobject res = NULL; |
77 |
if (!obj.is_null()) { |
|
78 |
// ignore null handles |
|
79 |
MutexLocker ml(JNIGlobalHandle_lock); |
|
80 |
assert(Universe::heap()->is_in_reserved(obj()), "sanity check"); |
|
81 |
res = _global_handles->allocate_handle(obj()); |
|
82 |
} else { |
|
83 |
CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); |
|
84 |
} |
|
85 |
||
86 |
return res; |
|
87 |
} |
|
88 |
||
89 |
||
90 |
jobject JNIHandles::make_weak_global(Handle obj) { |
|
5896
c60924a89622
6656830: assert((*p)->is_oop(),"expected an oop while scanning weak refs")
never
parents:
5547
diff
changeset
|
91 |
assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); |
1 | 92 |
jobject res = NULL; |
93 |
if (!obj.is_null()) { |
|
94 |
// ignore null handles |
|
95 |
MutexLocker ml(JNIGlobalHandle_lock); |
|
96 |
assert(Universe::heap()->is_in_reserved(obj()), "sanity check"); |
|
97 |
res = _weak_global_handles->allocate_handle(obj()); |
|
98 |
} else { |
|
99 |
CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); |
|
100 |
} |
|
101 |
return res; |
|
102 |
} |
|
103 |
||
104 |
||
105 |
void JNIHandles::destroy_global(jobject handle) { |
|
106 |
if (handle != NULL) { |
|
107 |
assert(is_global_handle(handle), "Invalid delete of global JNI handle"); |
|
108 |
*((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
||
113 |
void JNIHandles::destroy_weak_global(jobject handle) { |
|
114 |
if (handle != NULL) { |
|
115 |
assert(!CheckJNICalls || is_weak_global_handle(handle), "Invalid delete of weak global JNI handle"); |
|
116 |
*((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
||
121 |
void JNIHandles::oops_do(OopClosure* f) { |
|
122 |
f->do_oop(&_deleted_handle); |
|
123 |
_global_handles->oops_do(f); |
|
124 |
} |
|
125 |
||
126 |
||
32817
acc2744fd84b
8136627: Backout JDK-8133818 Additional number of processed references printed with -XX:+PrintReferenceGC after JDK-8047125
kbarrett
parents:
32607
diff
changeset
|
127 |
void JNIHandles::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) { |
acc2744fd84b
8136627: Backout JDK-8133818 Additional number of processed references printed with -XX:+PrintReferenceGC after JDK-8047125
kbarrett
parents:
32607
diff
changeset
|
128 |
_weak_global_handles->weak_oops_do(is_alive, f); |
1 | 129 |
} |
130 |
||
131 |
||
37106 | 132 |
void JNIHandles::weak_oops_do(OopClosure* f) { |
133 |
AlwaysTrueClosure always_true; |
|
134 |
weak_oops_do(&always_true, f); |
|
135 |
} |
|
136 |
||
137 |
||
1 | 138 |
void JNIHandles::initialize() { |
139 |
_global_handles = JNIHandleBlock::allocate_block(); |
|
140 |
_weak_global_handles = JNIHandleBlock::allocate_block(); |
|
141 |
EXCEPTION_MARK; |
|
142 |
// We will never reach the CATCH below since Exceptions::_throw will cause |
|
143 |
// the VM to exit if an exception is thrown during initialization |
|
13728
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
10565
diff
changeset
|
144 |
Klass* k = SystemDictionary::Object_klass(); |
882756847a04
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
10565
diff
changeset
|
145 |
_deleted_handle = InstanceKlass::cast(k)->allocate_instance(CATCH); |
1 | 146 |
} |
147 |
||
148 |
||
149 |
bool JNIHandles::is_local_handle(Thread* thread, jobject handle) { |
|
150 |
JNIHandleBlock* block = thread->active_handles(); |
|
151 |
||
152 |
// Look back past possible native calls to jni_PushLocalFrame. |
|
153 |
while (block != NULL) { |
|
154 |
if (block->chain_contains(handle)) { |
|
155 |
return true; |
|
156 |
} |
|
157 |
block = block->pop_frame_link(); |
|
158 |
} |
|
159 |
return false; |
|
160 |
} |
|
161 |
||
162 |
||
163 |
// Determine if the handle is somewhere in the current thread's stack. |
|
164 |
// We easily can't isolate any particular stack frame the handle might |
|
165 |
// come from, so we'll check the whole stack. |
|
166 |
||
167 |
bool JNIHandles::is_frame_handle(JavaThread* thr, jobject obj) { |
|
168 |
// If there is no java frame, then this must be top level code, such |
|
169 |
// as the java command executable, in which case, this type of handle |
|
170 |
// is not permitted. |
|
171 |
return (thr->has_last_Java_frame() && |
|
172 |
(void*)obj < (void*)thr->stack_base() && |
|
173 |
(void*)obj >= (void*)thr->last_Java_sp()); |
|
174 |
} |
|
175 |
||
176 |
||
177 |
bool JNIHandles::is_global_handle(jobject handle) { |
|
178 |
return _global_handles->chain_contains(handle); |
|
179 |
} |
|
180 |
||
181 |
||
182 |
bool JNIHandles::is_weak_global_handle(jobject handle) { |
|
183 |
return _weak_global_handles->chain_contains(handle); |
|
184 |
} |
|
185 |
||
186 |
long JNIHandles::global_handle_memory_usage() { |
|
187 |
return _global_handles->memory_usage(); |
|
188 |
} |
|
189 |
||
190 |
long JNIHandles::weak_global_handle_memory_usage() { |
|
191 |
return _weak_global_handles->memory_usage(); |
|
192 |
} |
|
193 |
||
194 |
||
195 |
class CountHandleClosure: public OopClosure { |
|
196 |
private: |
|
197 |
int _count; |
|
198 |
public: |
|
199 |
CountHandleClosure(): _count(0) {} |
|
22530
d1a70d79c50e
6651256: jstack: DeleteGlobalRef method call doesn't lead to descreasing of global refs count shown by jstack
minqi
parents:
22234
diff
changeset
|
200 |
virtual void do_oop(oop* ooph) { |
d1a70d79c50e
6651256: jstack: DeleteGlobalRef method call doesn't lead to descreasing of global refs count shown by jstack
minqi
parents:
22234
diff
changeset
|
201 |
if (*ooph != JNIHandles::deleted_handle()) { |
d1a70d79c50e
6651256: jstack: DeleteGlobalRef method call doesn't lead to descreasing of global refs count shown by jstack
minqi
parents:
22234
diff
changeset
|
202 |
_count++; |
d1a70d79c50e
6651256: jstack: DeleteGlobalRef method call doesn't lead to descreasing of global refs count shown by jstack
minqi
parents:
22234
diff
changeset
|
203 |
} |
1 | 204 |
} |
360
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
205 |
virtual void do_oop(narrowOop* unused) { ShouldNotReachHere(); } |
1 | 206 |
int count() { return _count; } |
207 |
}; |
|
208 |
||
209 |
// We assume this is called at a safepoint: no lock is needed. |
|
210 |
void JNIHandles::print_on(outputStream* st) { |
|
211 |
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); |
|
212 |
assert(_global_handles != NULL && _weak_global_handles != NULL, |
|
213 |
"JNIHandles not initialized"); |
|
214 |
||
215 |
CountHandleClosure global_handle_count; |
|
216 |
oops_do(&global_handle_count); |
|
37106 | 217 |
weak_oops_do(&global_handle_count); |
1 | 218 |
|
219 |
st->print_cr("JNI global references: %d", global_handle_count.count()); |
|
220 |
st->cr(); |
|
221 |
st->flush(); |
|
222 |
} |
|
223 |
||
224 |
class VerifyHandleClosure: public OopClosure { |
|
225 |
public: |
|
360
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
226 |
virtual void do_oop(oop* root) { |
1 | 227 |
(*root)->verify(); |
228 |
} |
|
360
21d113ecbf6a
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
1
diff
changeset
|
229 |
virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); } |
1 | 230 |
}; |
231 |
||
232 |
void JNIHandles::verify() { |
|
233 |
VerifyHandleClosure verify_handle; |
|
234 |
||
235 |
oops_do(&verify_handle); |
|
37106 | 236 |
weak_oops_do(&verify_handle); |
1 | 237 |
} |
238 |
||
239 |
||
240 |
||
241 |
void jni_handles_init() { |
|
242 |
JNIHandles::initialize(); |
|
243 |
} |
|
244 |
||
245 |
||
246 |
int JNIHandleBlock::_blocks_allocated = 0; |
|
247 |
JNIHandleBlock* JNIHandleBlock::_block_free_list = NULL; |
|
248 |
#ifndef PRODUCT |
|
249 |
JNIHandleBlock* JNIHandleBlock::_block_list = NULL; |
|
250 |
#endif |
|
251 |
||
252 |
||
253 |
void JNIHandleBlock::zap() { |
|
254 |
// Zap block values |
|
255 |
_top = 0; |
|
256 |
for (int index = 0; index < block_size_in_oops; index++) { |
|
257 |
_handles[index] = badJNIHandle; |
|
258 |
} |
|
259 |
} |
|
260 |
||
261 |
JNIHandleBlock* JNIHandleBlock::allocate_block(Thread* thread) { |
|
262 |
assert(thread == NULL || thread == Thread::current(), "sanity check"); |
|
263 |
JNIHandleBlock* block; |
|
264 |
// Check the thread-local free list for a block so we don't |
|
265 |
// have to acquire a mutex. |
|
266 |
if (thread != NULL && thread->free_handle_block() != NULL) { |
|
267 |
block = thread->free_handle_block(); |
|
268 |
thread->set_free_handle_block(block->_next); |
|
269 |
} |
|
270 |
else { |
|
271 |
// locking with safepoint checking introduces a potential deadlock: |
|
272 |
// - we would hold JNIHandleBlockFreeList_lock and then Threads_lock |
|
273 |
// - another would hold Threads_lock (jni_AttachCurrentThread) and then |
|
274 |
// JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block) |
|
275 |
MutexLockerEx ml(JNIHandleBlockFreeList_lock, |
|
276 |
Mutex::_no_safepoint_check_flag); |
|
277 |
if (_block_free_list == NULL) { |
|
278 |
// Allocate new block |
|
279 |
block = new JNIHandleBlock(); |
|
280 |
_blocks_allocated++; |
|
281 |
if (ZapJNIHandleArea) block->zap(); |
|
282 |
#ifndef PRODUCT |
|
283 |
// Link new block to list of all allocated blocks |
|
284 |
block->_block_list_link = _block_list; |
|
285 |
_block_list = block; |
|
286 |
#endif |
|
287 |
} else { |
|
288 |
// Get block from free list |
|
289 |
block = _block_free_list; |
|
290 |
_block_free_list = _block_free_list->_next; |
|
291 |
} |
|
292 |
} |
|
293 |
block->_top = 0; |
|
294 |
block->_next = NULL; |
|
295 |
block->_pop_frame_link = NULL; |
|
25058
4542f853c2ac
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents:
24424
diff
changeset
|
296 |
block->_planned_capacity = block_size_in_oops; |
1 | 297 |
// _last, _free_list & _allocate_before_rebuild initialized in allocate_handle |
298 |
debug_only(block->_last = NULL); |
|
299 |
debug_only(block->_free_list = NULL); |
|
300 |
debug_only(block->_allocate_before_rebuild = -1); |
|
301 |
return block; |
|
302 |
} |
|
303 |
||
304 |
||
305 |
void JNIHandleBlock::release_block(JNIHandleBlock* block, Thread* thread) { |
|
306 |
assert(thread == NULL || thread == Thread::current(), "sanity check"); |
|
307 |
JNIHandleBlock* pop_frame_link = block->pop_frame_link(); |
|
308 |
// Put returned block at the beginning of the thread-local free list. |
|
309 |
// Note that if thread == NULL, we use it as an implicit argument that |
|
310 |
// we _don't_ want the block to be kept on the free_handle_block. |
|
311 |
// See for instance JavaThread::exit(). |
|
312 |
if (thread != NULL ) { |
|
313 |
if (ZapJNIHandleArea) block->zap(); |
|
314 |
JNIHandleBlock* freelist = thread->free_handle_block(); |
|
315 |
block->_pop_frame_link = NULL; |
|
316 |
thread->set_free_handle_block(block); |
|
317 |
||
318 |
// Add original freelist to end of chain |
|
319 |
if ( freelist != NULL ) { |
|
320 |
while ( block->_next != NULL ) block = block->_next; |
|
321 |
block->_next = freelist; |
|
322 |
} |
|
323 |
block = NULL; |
|
324 |
} |
|
325 |
if (block != NULL) { |
|
326 |
// Return blocks to free list |
|
327 |
// locking with safepoint checking introduces a potential deadlock: |
|
328 |
// - we would hold JNIHandleBlockFreeList_lock and then Threads_lock |
|
329 |
// - another would hold Threads_lock (jni_AttachCurrentThread) and then |
|
330 |
// JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block) |
|
331 |
MutexLockerEx ml(JNIHandleBlockFreeList_lock, |
|
332 |
Mutex::_no_safepoint_check_flag); |
|
333 |
while (block != NULL) { |
|
334 |
if (ZapJNIHandleArea) block->zap(); |
|
335 |
JNIHandleBlock* next = block->_next; |
|
336 |
block->_next = _block_free_list; |
|
337 |
_block_free_list = block; |
|
338 |
block = next; |
|
339 |
} |
|
340 |
} |
|
341 |
if (pop_frame_link != NULL) { |
|
342 |
// As a sanity check we release blocks pointed to by the pop_frame_link. |
|
343 |
// This should never happen (only if PopLocalFrame is not called the |
|
344 |
// correct number of times). |
|
345 |
release_block(pop_frame_link, thread); |
|
346 |
} |
|
347 |
} |
|
348 |
||
349 |
||
350 |
void JNIHandleBlock::oops_do(OopClosure* f) { |
|
351 |
JNIHandleBlock* current_chain = this; |
|
352 |
// Iterate over chain of blocks, followed by chains linked through the |
|
353 |
// pop frame links. |
|
354 |
while (current_chain != NULL) { |
|
355 |
for (JNIHandleBlock* current = current_chain; current != NULL; |
|
356 |
current = current->_next) { |
|
357 |
assert(current == current_chain || current->pop_frame_link() == NULL, |
|
358 |
"only blocks first in chain should have pop frame link set"); |
|
359 |
for (int index = 0; index < current->_top; index++) { |
|
360 |
oop* root = &(current->_handles)[index]; |
|
361 |
oop value = *root; |
|
362 |
// traverse heap pointers only, not deleted handles or free list |
|
363 |
// pointers |
|
364 |
if (value != NULL && Universe::heap()->is_in_reserved(value)) { |
|
365 |
f->do_oop(root); |
|
366 |
} |
|
367 |
} |
|
368 |
// the next handle block is valid only if current block is full |
|
369 |
if (current->_top < block_size_in_oops) { |
|
370 |
break; |
|
371 |
} |
|
372 |
} |
|
373 |
current_chain = current_chain->pop_frame_link(); |
|
374 |
} |
|
375 |
} |
|
376 |
||
377 |
||
32817
acc2744fd84b
8136627: Backout JDK-8133818 Additional number of processed references printed with -XX:+PrintReferenceGC after JDK-8047125
kbarrett
parents:
32607
diff
changeset
|
378 |
void JNIHandleBlock::weak_oops_do(BoolObjectClosure* is_alive, |
acc2744fd84b
8136627: Backout JDK-8133818 Additional number of processed references printed with -XX:+PrintReferenceGC after JDK-8047125
kbarrett
parents:
32607
diff
changeset
|
379 |
OopClosure* f) { |
1 | 380 |
for (JNIHandleBlock* current = this; current != NULL; current = current->_next) { |
381 |
assert(current->pop_frame_link() == NULL, |
|
382 |
"blocks holding weak global JNI handles should not have pop frame link set"); |
|
383 |
for (int index = 0; index < current->_top; index++) { |
|
384 |
oop* root = &(current->_handles)[index]; |
|
385 |
oop value = *root; |
|
386 |
// traverse heap pointers only, not deleted handles or free list pointers |
|
387 |
if (value != NULL && Universe::heap()->is_in_reserved(value)) { |
|
388 |
if (is_alive->do_object_b(value)) { |
|
389 |
// The weakly referenced object is alive, update pointer |
|
390 |
f->do_oop(root); |
|
391 |
} else { |
|
392 |
// The weakly referenced object is not alive, clear the reference by storing NULL |
|
35061 | 393 |
log_develop_trace(gc, ref)("Clearing JNI weak reference (" INTPTR_FORMAT ")", p2i(root)); |
1 | 394 |
*root = NULL; |
395 |
} |
|
396 |
} |
|
397 |
} |
|
398 |
// the next handle block is valid only if current block is full |
|
399 |
if (current->_top < block_size_in_oops) { |
|
400 |
break; |
|
401 |
} |
|
402 |
} |
|
7896
08aadd7aa3ee
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
7397
diff
changeset
|
403 |
|
08aadd7aa3ee
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
7397
diff
changeset
|
404 |
/* |
7918
ce1e4ae77aea
7012505: BreakpointWithFullGC.sh fails with Internal Error (src/share/vm/oops/methodOop.cpp:220)
kamg
parents:
7896
diff
changeset
|
405 |
* JVMTI data structures may also contain weak oops. The iteration of them |
ce1e4ae77aea
7012505: BreakpointWithFullGC.sh fails with Internal Error (src/share/vm/oops/methodOop.cpp:220)
kamg
parents:
7896
diff
changeset
|
406 |
* is placed here so that we don't need to add it to each of the collectors. |
7896
08aadd7aa3ee
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
7397
diff
changeset
|
407 |
*/ |
32817
acc2744fd84b
8136627: Backout JDK-8133818 Additional number of processed references printed with -XX:+PrintReferenceGC after JDK-8047125
kbarrett
parents:
32607
diff
changeset
|
408 |
JvmtiExport::weak_oops_do(is_alive, f); |
1 | 409 |
} |
410 |
||
411 |
||
412 |
jobject JNIHandleBlock::allocate_handle(oop obj) { |
|
413 |
assert(Universe::heap()->is_in_reserved(obj), "sanity check"); |
|
414 |
if (_top == 0) { |
|
415 |
// This is the first allocation or the initial block got zapped when |
|
416 |
// entering a native function. If we have any following blocks they are |
|
417 |
// not valid anymore. |
|
418 |
for (JNIHandleBlock* current = _next; current != NULL; |
|
419 |
current = current->_next) { |
|
420 |
assert(current->_last == NULL, "only first block should have _last set"); |
|
421 |
assert(current->_free_list == NULL, |
|
422 |
"only first block should have _free_list set"); |
|
423 |
current->_top = 0; |
|
424 |
if (ZapJNIHandleArea) current->zap(); |
|
425 |
} |
|
426 |
// Clear initial block |
|
427 |
_free_list = NULL; |
|
428 |
_allocate_before_rebuild = 0; |
|
429 |
_last = this; |
|
430 |
if (ZapJNIHandleArea) zap(); |
|
431 |
} |
|
432 |
||
433 |
// Try last block |
|
434 |
if (_last->_top < block_size_in_oops) { |
|
435 |
oop* handle = &(_last->_handles)[_last->_top++]; |
|
436 |
*handle = obj; |
|
437 |
return (jobject) handle; |
|
438 |
} |
|
439 |
||
440 |
// Try free list |
|
441 |
if (_free_list != NULL) { |
|
442 |
oop* handle = _free_list; |
|
443 |
_free_list = (oop*) *_free_list; |
|
444 |
*handle = obj; |
|
445 |
return (jobject) handle; |
|
446 |
} |
|
447 |
// Check if unused block follow last |
|
448 |
if (_last->_next != NULL) { |
|
449 |
// update last and retry |
|
450 |
_last = _last->_next; |
|
451 |
return allocate_handle(obj); |
|
452 |
} |
|
453 |
||
454 |
// No space available, we have to rebuild free list or expand |
|
455 |
if (_allocate_before_rebuild == 0) { |
|
456 |
rebuild_free_list(); // updates _allocate_before_rebuild counter |
|
457 |
} else { |
|
458 |
// Append new block |
|
459 |
Thread* thread = Thread::current(); |
|
460 |
Handle obj_handle(thread, obj); |
|
22551 | 461 |
// This can block, so we need to preserve obj across call. |
1 | 462 |
_last->_next = JNIHandleBlock::allocate_block(thread); |
463 |
_last = _last->_next; |
|
464 |
_allocate_before_rebuild--; |
|
465 |
obj = obj_handle(); |
|
466 |
} |
|
467 |
return allocate_handle(obj); // retry |
|
468 |
} |
|
469 |
||
470 |
||
471 |
void JNIHandleBlock::rebuild_free_list() { |
|
472 |
assert(_allocate_before_rebuild == 0 && _free_list == NULL, "just checking"); |
|
473 |
int free = 0; |
|
474 |
int blocks = 0; |
|
475 |
for (JNIHandleBlock* current = this; current != NULL; current = current->_next) { |
|
476 |
for (int index = 0; index < current->_top; index++) { |
|
477 |
oop* handle = &(current->_handles)[index]; |
|
478 |
if (*handle == JNIHandles::deleted_handle()) { |
|
479 |
// this handle was cleared out by a delete call, reuse it |
|
480 |
*handle = (oop) _free_list; |
|
481 |
_free_list = handle; |
|
482 |
free++; |
|
483 |
} |
|
484 |
} |
|
485 |
// we should not rebuild free list if there are unused handles at the end |
|
486 |
assert(current->_top == block_size_in_oops, "just checking"); |
|
487 |
blocks++; |
|
488 |
} |
|
489 |
// Heuristic: if more than half of the handles are free we rebuild next time |
|
490 |
// as well, otherwise we append a corresponding number of new blocks before |
|
491 |
// attempting a free list rebuild again. |
|
492 |
int total = blocks * block_size_in_oops; |
|
493 |
int extra = total - 2*free; |
|
494 |
if (extra > 0) { |
|
495 |
// Not as many free handles as we would like - compute number of new blocks to append |
|
496 |
_allocate_before_rebuild = (extra + block_size_in_oops - 1) / block_size_in_oops; |
|
497 |
} |
|
498 |
} |
|
499 |
||
500 |
||
501 |
bool JNIHandleBlock::contains(jobject handle) const { |
|
502 |
return ((jobject)&_handles[0] <= handle && handle<(jobject)&_handles[_top]); |
|
503 |
} |
|
504 |
||
505 |
||
506 |
bool JNIHandleBlock::chain_contains(jobject handle) const { |
|
507 |
for (JNIHandleBlock* current = (JNIHandleBlock*) this; current != NULL; current = current->_next) { |
|
508 |
if (current->contains(handle)) { |
|
509 |
return true; |
|
510 |
} |
|
511 |
} |
|
512 |
return false; |
|
513 |
} |
|
514 |
||
515 |
||
516 |
int JNIHandleBlock::length() const { |
|
517 |
int result = 1; |
|
518 |
for (JNIHandleBlock* current = _next; current != NULL; current = current->_next) { |
|
519 |
result++; |
|
520 |
} |
|
521 |
return result; |
|
522 |
} |
|
523 |
||
25058
4542f853c2ac
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents:
24424
diff
changeset
|
524 |
const size_t JNIHandleBlock::get_number_of_live_handles() { |
4542f853c2ac
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents:
24424
diff
changeset
|
525 |
CountHandleClosure counter; |
4542f853c2ac
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents:
24424
diff
changeset
|
526 |
oops_do(&counter); |
4542f853c2ac
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents:
24424
diff
changeset
|
527 |
return counter.count(); |
4542f853c2ac
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents:
24424
diff
changeset
|
528 |
} |
4542f853c2ac
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
dsimms
parents:
24424
diff
changeset
|
529 |
|
22551 | 530 |
// This method is not thread-safe, i.e., must be called while holding a lock on the |
1 | 531 |
// structure. |
532 |
long JNIHandleBlock::memory_usage() const { |
|
533 |
return length() * sizeof(JNIHandleBlock); |
|
534 |
} |
|
535 |
||
536 |
||
537 |
#ifndef PRODUCT |
|
538 |
||
539 |
bool JNIHandleBlock::any_contains(jobject handle) { |
|
540 |
for (JNIHandleBlock* current = _block_list; current != NULL; current = current->_block_list_link) { |
|
541 |
if (current->contains(handle)) { |
|
542 |
return true; |
|
543 |
} |
|
544 |
} |
|
545 |
return false; |
|
546 |
} |
|
547 |
||
548 |
void JNIHandleBlock::print_statistics() { |
|
549 |
int used_blocks = 0; |
|
550 |
int free_blocks = 0; |
|
551 |
int used_handles = 0; |
|
552 |
int free_handles = 0; |
|
553 |
JNIHandleBlock* block = _block_list; |
|
554 |
while (block != NULL) { |
|
555 |
if (block->_top > 0) { |
|
556 |
used_blocks++; |
|
557 |
} else { |
|
558 |
free_blocks++; |
|
559 |
} |
|
560 |
used_handles += block->_top; |
|
561 |
free_handles += (block_size_in_oops - block->_top); |
|
562 |
block = block->_block_list_link; |
|
563 |
} |
|
564 |
tty->print_cr("JNIHandleBlocks statistics"); |
|
565 |
tty->print_cr("- blocks allocated: %d", used_blocks + free_blocks); |
|
566 |
tty->print_cr("- blocks in use: %d", used_blocks); |
|
567 |
tty->print_cr("- blocks free: %d", free_blocks); |
|
568 |
tty->print_cr("- handles in use: %d", used_handles); |
|
569 |
tty->print_cr("- handles free: %d", free_handles); |
|
570 |
} |
|
571 |
||
572 |
#endif |