Mercurial > hg > graal-jvmci-8
annotate src/share/vm/runtime/jniHandles.cpp @ 24102:d1a869fb73c2
Performance fix for JNI handle allocation.
author | Christian Haeubl <christian.haeubl@oracle.com> |
---|---|
date | Tue, 14 Feb 2017 11:10:26 +0100 |
parents | dd9cc155639c |
children |
rev | line source |
---|---|
0 | 1 /* |
22877
d68158e12cea
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
poonam
parents:
17937
diff
changeset
|
2 * Copyright (c) 1998, 2015, 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:
1142
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1142
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:
1142
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
1972 | 25 #include "precompiled.hpp" |
26 #include "classfile/systemDictionary.hpp" | |
27 #include "oops/oop.inline.hpp" | |
2147
9afee0b9fc1d
7012505: BreakpointWithFullGC.sh fails with Internal Error (src/share/vm/oops/methodOop.cpp:220)
kamg
parents:
2125
diff
changeset
|
28 #include "prims/jvmtiExport.hpp" |
1972 | 29 #include "runtime/jniHandles.hpp" |
30 #include "runtime/mutexLocker.hpp" | |
7180
f34d701e952e
8003935: Simplify the needed includes for using Thread::current()
stefank
parents:
6725
diff
changeset
|
31 #include "runtime/thread.inline.hpp" |
0 | 32 |
17937
78bbf4d43a14
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
14223
diff
changeset
|
33 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC |
0 | 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) { | |
1616
38e8278318ca
6656830: assert((*p)->is_oop(),"expected an oop while scanning weak refs")
never
parents:
1552
diff
changeset
|
75 assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); |
0 | 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) { | |
1616
38e8278318ca
6656830: assert((*p)->is_oop(),"expected an oop while scanning weak refs")
never
parents:
1552
diff
changeset
|
91 assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); |
0 | 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 | |
127 void JNIHandles::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) { | |
128 _weak_global_handles->weak_oops_do(is_alive, f); | |
129 } | |
130 | |
131 | |
132 void JNIHandles::initialize() { | |
133 _global_handles = JNIHandleBlock::allocate_block(); | |
134 _weak_global_handles = JNIHandleBlock::allocate_block(); | |
135 EXCEPTION_MARK; | |
136 // We will never reach the CATCH below since Exceptions::_throw will cause | |
137 // the VM to exit if an exception is thrown during initialization | |
6725
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3960
diff
changeset
|
138 Klass* k = SystemDictionary::Object_klass(); |
da91efe96a93
6964458: Reimplement class meta-data storage to use native memory
coleenp
parents:
3960
diff
changeset
|
139 _deleted_handle = InstanceKlass::cast(k)->allocate_instance(CATCH); |
0 | 140 } |
141 | |
142 | |
143 bool JNIHandles::is_local_handle(Thread* thread, jobject handle) { | |
144 JNIHandleBlock* block = thread->active_handles(); | |
145 | |
146 // Look back past possible native calls to jni_PushLocalFrame. | |
147 while (block != NULL) { | |
148 if (block->chain_contains(handle)) { | |
149 return true; | |
150 } | |
151 block = block->pop_frame_link(); | |
152 } | |
153 return false; | |
154 } | |
155 | |
156 | |
157 // Determine if the handle is somewhere in the current thread's stack. | |
158 // We easily can't isolate any particular stack frame the handle might | |
159 // come from, so we'll check the whole stack. | |
160 | |
161 bool JNIHandles::is_frame_handle(JavaThread* thr, jobject obj) { | |
162 // If there is no java frame, then this must be top level code, such | |
163 // as the java command executable, in which case, this type of handle | |
164 // is not permitted. | |
165 return (thr->has_last_Java_frame() && | |
166 (void*)obj < (void*)thr->stack_base() && | |
167 (void*)obj >= (void*)thr->last_Java_sp()); | |
168 } | |
169 | |
170 | |
171 bool JNIHandles::is_global_handle(jobject handle) { | |
172 return _global_handles->chain_contains(handle); | |
173 } | |
174 | |
175 | |
176 bool JNIHandles::is_weak_global_handle(jobject handle) { | |
177 return _weak_global_handles->chain_contains(handle); | |
178 } | |
179 | |
180 long JNIHandles::global_handle_memory_usage() { | |
181 return _global_handles->memory_usage(); | |
182 } | |
183 | |
184 long JNIHandles::weak_global_handle_memory_usage() { | |
185 return _weak_global_handles->memory_usage(); | |
186 } | |
187 | |
188 | |
189 class AlwaysAliveClosure: public BoolObjectClosure { | |
190 public: | |
191 bool do_object_b(oop obj) { return true; } | |
192 }; | |
193 | |
194 class CountHandleClosure: public OopClosure { | |
195 private: | |
196 int _count; | |
197 public: | |
198 CountHandleClosure(): _count(0) {} | |
113
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
199 virtual void do_oop(oop* unused) { |
0 | 200 _count++; |
201 } | |
113
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
202 virtual void do_oop(narrowOop* unused) { ShouldNotReachHere(); } |
0 | 203 int count() { return _count; } |
204 }; | |
205 | |
206 // We assume this is called at a safepoint: no lock is needed. | |
207 void JNIHandles::print_on(outputStream* st) { | |
208 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); | |
209 assert(_global_handles != NULL && _weak_global_handles != NULL, | |
210 "JNIHandles not initialized"); | |
211 | |
212 CountHandleClosure global_handle_count; | |
213 AlwaysAliveClosure always_alive; | |
214 oops_do(&global_handle_count); | |
215 weak_oops_do(&always_alive, &global_handle_count); | |
216 | |
217 st->print_cr("JNI global references: %d", global_handle_count.count()); | |
218 st->cr(); | |
219 st->flush(); | |
220 } | |
221 | |
222 class VerifyHandleClosure: public OopClosure { | |
223 public: | |
113
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
224 virtual void do_oop(oop* root) { |
0 | 225 (*root)->verify(); |
226 } | |
113
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
227 virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); } |
0 | 228 }; |
229 | |
230 void JNIHandles::verify() { | |
231 VerifyHandleClosure verify_handle; | |
232 AlwaysAliveClosure always_alive; | |
233 | |
234 oops_do(&verify_handle); | |
235 weak_oops_do(&always_alive, &verify_handle); | |
236 } | |
237 | |
238 | |
239 | |
240 void jni_handles_init() { | |
241 JNIHandles::initialize(); | |
242 } | |
243 | |
244 | |
245 int JNIHandleBlock::_blocks_allocated = 0; | |
246 JNIHandleBlock* JNIHandleBlock::_block_free_list = NULL; | |
247 #ifndef PRODUCT | |
248 JNIHandleBlock* JNIHandleBlock::_block_list = NULL; | |
249 #endif | |
250 | |
251 | |
252 void JNIHandleBlock::zap() { | |
253 // Zap block values | |
254 _top = 0; | |
255 for (int index = 0; index < block_size_in_oops; index++) { | |
256 _handles[index] = badJNIHandle; | |
257 } | |
258 } | |
259 | |
260 JNIHandleBlock* JNIHandleBlock::allocate_block(Thread* thread) { | |
261 assert(thread == NULL || thread == Thread::current(), "sanity check"); | |
262 JNIHandleBlock* block; | |
263 // Check the thread-local free list for a block so we don't | |
264 // have to acquire a mutex. | |
265 if (thread != NULL && thread->free_handle_block() != NULL) { | |
266 block = thread->free_handle_block(); | |
267 thread->set_free_handle_block(block->_next); | |
268 } | |
269 else { | |
270 // locking with safepoint checking introduces a potential deadlock: | |
271 // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock | |
272 // - another would hold Threads_lock (jni_AttachCurrentThread) and then | |
273 // JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block) | |
274 MutexLockerEx ml(JNIHandleBlockFreeList_lock, | |
275 Mutex::_no_safepoint_check_flag); | |
276 if (_block_free_list == NULL) { | |
277 // Allocate new block | |
278 block = new JNIHandleBlock(); | |
279 _blocks_allocated++; | |
280 if (TraceJNIHandleAllocation) { | |
281 tty->print_cr("JNIHandleBlock " INTPTR_FORMAT " allocated (%d total blocks)", | |
282 block, _blocks_allocated); | |
283 } | |
284 if (ZapJNIHandleArea) block->zap(); | |
285 #ifndef PRODUCT | |
286 // Link new block to list of all allocated blocks | |
287 block->_block_list_link = _block_list; | |
288 _block_list = block; | |
289 #endif | |
290 } else { | |
291 // Get block from free list | |
292 block = _block_free_list; | |
293 _block_free_list = _block_free_list->_next; | |
294 } | |
295 } | |
296 block->_top = 0; | |
297 block->_next = NULL; | |
298 block->_pop_frame_link = NULL; | |
22877
d68158e12cea
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
poonam
parents:
17937
diff
changeset
|
299 block->_planned_capacity = block_size_in_oops; |
0 | 300 // _last, _free_list & _allocate_before_rebuild initialized in allocate_handle |
301 debug_only(block->_last = NULL); | |
302 debug_only(block->_free_list = NULL); | |
303 debug_only(block->_allocate_before_rebuild = -1); | |
304 return block; | |
305 } | |
306 | |
307 | |
308 void JNIHandleBlock::release_block(JNIHandleBlock* block, Thread* thread) { | |
309 assert(thread == NULL || thread == Thread::current(), "sanity check"); | |
310 JNIHandleBlock* pop_frame_link = block->pop_frame_link(); | |
311 // Put returned block at the beginning of the thread-local free list. | |
312 // Note that if thread == NULL, we use it as an implicit argument that | |
313 // we _don't_ want the block to be kept on the free_handle_block. | |
314 // See for instance JavaThread::exit(). | |
315 if (thread != NULL ) { | |
316 if (ZapJNIHandleArea) block->zap(); | |
317 JNIHandleBlock* freelist = thread->free_handle_block(); | |
318 block->_pop_frame_link = NULL; | |
319 thread->set_free_handle_block(block); | |
320 | |
321 // Add original freelist to end of chain | |
322 if ( freelist != NULL ) { | |
323 while ( block->_next != NULL ) block = block->_next; | |
324 block->_next = freelist; | |
325 } | |
326 block = NULL; | |
327 } | |
328 if (block != NULL) { | |
329 // Return blocks to free list | |
330 // locking with safepoint checking introduces a potential deadlock: | |
331 // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock | |
332 // - another would hold Threads_lock (jni_AttachCurrentThread) and then | |
333 // JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block) | |
334 MutexLockerEx ml(JNIHandleBlockFreeList_lock, | |
335 Mutex::_no_safepoint_check_flag); | |
336 while (block != NULL) { | |
337 if (ZapJNIHandleArea) block->zap(); | |
338 JNIHandleBlock* next = block->_next; | |
339 block->_next = _block_free_list; | |
340 _block_free_list = block; | |
341 block = next; | |
342 } | |
343 } | |
344 if (pop_frame_link != NULL) { | |
345 // As a sanity check we release blocks pointed to by the pop_frame_link. | |
346 // This should never happen (only if PopLocalFrame is not called the | |
347 // correct number of times). | |
348 release_block(pop_frame_link, thread); | |
349 } | |
350 } | |
351 | |
352 | |
353 void JNIHandleBlock::oops_do(OopClosure* f) { | |
354 JNIHandleBlock* current_chain = this; | |
355 // Iterate over chain of blocks, followed by chains linked through the | |
356 // pop frame links. | |
357 while (current_chain != NULL) { | |
358 for (JNIHandleBlock* current = current_chain; current != NULL; | |
359 current = current->_next) { | |
360 assert(current == current_chain || current->pop_frame_link() == NULL, | |
361 "only blocks first in chain should have pop frame link set"); | |
362 for (int index = 0; index < current->_top; index++) { | |
363 oop* root = &(current->_handles)[index]; | |
364 oop value = *root; | |
365 // traverse heap pointers only, not deleted handles or free list | |
366 // pointers | |
367 if (value != NULL && Universe::heap()->is_in_reserved(value)) { | |
368 f->do_oop(root); | |
369 } | |
370 } | |
371 // the next handle block is valid only if current block is full | |
372 if (current->_top < block_size_in_oops) { | |
373 break; | |
374 } | |
375 } | |
376 current_chain = current_chain->pop_frame_link(); | |
377 } | |
378 } | |
379 | |
380 | |
381 void JNIHandleBlock::weak_oops_do(BoolObjectClosure* is_alive, | |
382 OopClosure* f) { | |
383 for (JNIHandleBlock* current = this; current != NULL; current = current->_next) { | |
384 assert(current->pop_frame_link() == NULL, | |
385 "blocks holding weak global JNI handles should not have pop frame link set"); | |
386 for (int index = 0; index < current->_top; index++) { | |
387 oop* root = &(current->_handles)[index]; | |
388 oop value = *root; | |
389 // traverse heap pointers only, not deleted handles or free list pointers | |
390 if (value != NULL && Universe::heap()->is_in_reserved(value)) { | |
391 if (is_alive->do_object_b(value)) { | |
392 // The weakly referenced object is alive, update pointer | |
393 f->do_oop(root); | |
394 } else { | |
395 // The weakly referenced object is not alive, clear the reference by storing NULL | |
396 if (TraceReferenceGC) { | |
397 tty->print_cr("Clearing JNI weak reference (" INTPTR_FORMAT ")", root); | |
398 } | |
399 *root = NULL; | |
400 } | |
401 } | |
402 } | |
403 // the next handle block is valid only if current block is full | |
404 if (current->_top < block_size_in_oops) { | |
405 break; | |
406 } | |
407 } | |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
408 |
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
409 /* |
2147
9afee0b9fc1d
7012505: BreakpointWithFullGC.sh fails with Internal Error (src/share/vm/oops/methodOop.cpp:220)
kamg
parents:
2125
diff
changeset
|
410 * JVMTI data structures may also contain weak oops. The iteration of them |
9afee0b9fc1d
7012505: BreakpointWithFullGC.sh fails with Internal Error (src/share/vm/oops/methodOop.cpp:220)
kamg
parents:
2125
diff
changeset
|
411 * is placed here so that we don't need to add it to each of the collectors. |
2125
7246a374a9f2
6458402: 3 jvmti tests fail with CMS and +ExplicitGCInvokesConcurrent
kamg
parents:
1972
diff
changeset
|
412 */ |
2147
9afee0b9fc1d
7012505: BreakpointWithFullGC.sh fails with Internal Error (src/share/vm/oops/methodOop.cpp:220)
kamg
parents:
2125
diff
changeset
|
413 JvmtiExport::weak_oops_do(is_alive, f); |
0 | 414 } |
415 | |
416 | |
417 jobject JNIHandleBlock::allocate_handle(oop obj) { | |
418 assert(Universe::heap()->is_in_reserved(obj), "sanity check"); | |
419 if (_top == 0) { | |
420 // This is the first allocation or the initial block got zapped when | |
421 // entering a native function. If we have any following blocks they are | |
422 // not valid anymore. | |
24102
d1a869fb73c2
Performance fix for JNI handle allocation.
Christian Haeubl <christian.haeubl@oracle.com>
parents:
23286
diff
changeset
|
423 JNIHandleBlock* current = _next; |
d1a869fb73c2
Performance fix for JNI handle allocation.
Christian Haeubl <christian.haeubl@oracle.com>
parents:
23286
diff
changeset
|
424 while (current != NULL) { |
0 | 425 assert(current->_last == NULL, "only first block should have _last set"); |
426 assert(current->_free_list == NULL, | |
427 "only first block should have _free_list set"); | |
24102
d1a869fb73c2
Performance fix for JNI handle allocation.
Christian Haeubl <christian.haeubl@oracle.com>
parents:
23286
diff
changeset
|
428 |
d1a869fb73c2
Performance fix for JNI handle allocation.
Christian Haeubl <christian.haeubl@oracle.com>
parents:
23286
diff
changeset
|
429 // all blocks after the first free block must already be empty |
d1a869fb73c2
Performance fix for JNI handle allocation.
Christian Haeubl <christian.haeubl@oracle.com>
parents:
23286
diff
changeset
|
430 if (current->_top == 0) break; |
d1a869fb73c2
Performance fix for JNI handle allocation.
Christian Haeubl <christian.haeubl@oracle.com>
parents:
23286
diff
changeset
|
431 |
0 | 432 current->_top = 0; |
433 if (ZapJNIHandleArea) current->zap(); | |
24102
d1a869fb73c2
Performance fix for JNI handle allocation.
Christian Haeubl <christian.haeubl@oracle.com>
parents:
23286
diff
changeset
|
434 |
d1a869fb73c2
Performance fix for JNI handle allocation.
Christian Haeubl <christian.haeubl@oracle.com>
parents:
23286
diff
changeset
|
435 current = current->_next; |
0 | 436 } |
24102
d1a869fb73c2
Performance fix for JNI handle allocation.
Christian Haeubl <christian.haeubl@oracle.com>
parents:
23286
diff
changeset
|
437 |
d1a869fb73c2
Performance fix for JNI handle allocation.
Christian Haeubl <christian.haeubl@oracle.com>
parents:
23286
diff
changeset
|
438 #ifdef ASSERT |
d1a869fb73c2
Performance fix for JNI handle allocation.
Christian Haeubl <christian.haeubl@oracle.com>
parents:
23286
diff
changeset
|
439 while (current != NULL) { |
d1a869fb73c2
Performance fix for JNI handle allocation.
Christian Haeubl <christian.haeubl@oracle.com>
parents:
23286
diff
changeset
|
440 assert(current->_top == 0, "all blocks must already have been reset before"); |
d1a869fb73c2
Performance fix for JNI handle allocation.
Christian Haeubl <christian.haeubl@oracle.com>
parents:
23286
diff
changeset
|
441 current = current->_next; |
d1a869fb73c2
Performance fix for JNI handle allocation.
Christian Haeubl <christian.haeubl@oracle.com>
parents:
23286
diff
changeset
|
442 } |
d1a869fb73c2
Performance fix for JNI handle allocation.
Christian Haeubl <christian.haeubl@oracle.com>
parents:
23286
diff
changeset
|
443 #endif // ASSERT |
d1a869fb73c2
Performance fix for JNI handle allocation.
Christian Haeubl <christian.haeubl@oracle.com>
parents:
23286
diff
changeset
|
444 |
0 | 445 // Clear initial block |
446 _free_list = NULL; | |
447 _allocate_before_rebuild = 0; | |
448 _last = this; | |
449 if (ZapJNIHandleArea) zap(); | |
450 } | |
451 | |
452 // Try last block | |
453 if (_last->_top < block_size_in_oops) { | |
454 oop* handle = &(_last->_handles)[_last->_top++]; | |
455 *handle = obj; | |
456 return (jobject) handle; | |
457 } | |
458 | |
459 // Try free list | |
460 if (_free_list != NULL) { | |
461 oop* handle = _free_list; | |
462 _free_list = (oop*) *_free_list; | |
463 *handle = obj; | |
464 return (jobject) handle; | |
465 } | |
466 // Check if unused block follow last | |
467 if (_last->_next != NULL) { | |
468 // update last and retry | |
469 _last = _last->_next; | |
470 return allocate_handle(obj); | |
471 } | |
472 | |
473 // No space available, we have to rebuild free list or expand | |
474 if (_allocate_before_rebuild == 0) { | |
475 rebuild_free_list(); // updates _allocate_before_rebuild counter | |
476 } else { | |
477 // Append new block | |
478 Thread* thread = Thread::current(); | |
479 Handle obj_handle(thread, obj); | |
480 // This can block, so we need to preserve obj accross call. | |
481 _last->_next = JNIHandleBlock::allocate_block(thread); | |
482 _last = _last->_next; | |
483 _allocate_before_rebuild--; | |
484 obj = obj_handle(); | |
485 } | |
486 return allocate_handle(obj); // retry | |
487 } | |
488 | |
489 | |
490 void JNIHandleBlock::rebuild_free_list() { | |
491 assert(_allocate_before_rebuild == 0 && _free_list == NULL, "just checking"); | |
492 int free = 0; | |
493 int blocks = 0; | |
494 for (JNIHandleBlock* current = this; current != NULL; current = current->_next) { | |
495 for (int index = 0; index < current->_top; index++) { | |
496 oop* handle = &(current->_handles)[index]; | |
497 if (*handle == JNIHandles::deleted_handle()) { | |
498 // this handle was cleared out by a delete call, reuse it | |
499 *handle = (oop) _free_list; | |
500 _free_list = handle; | |
501 free++; | |
502 } | |
503 } | |
504 // we should not rebuild free list if there are unused handles at the end | |
505 assert(current->_top == block_size_in_oops, "just checking"); | |
506 blocks++; | |
507 } | |
508 // Heuristic: if more than half of the handles are free we rebuild next time | |
509 // as well, otherwise we append a corresponding number of new blocks before | |
510 // attempting a free list rebuild again. | |
511 int total = blocks * block_size_in_oops; | |
512 int extra = total - 2*free; | |
513 if (extra > 0) { | |
514 // Not as many free handles as we would like - compute number of new blocks to append | |
515 _allocate_before_rebuild = (extra + block_size_in_oops - 1) / block_size_in_oops; | |
516 } | |
517 if (TraceJNIHandleAllocation) { | |
518 tty->print_cr("Rebuild free list JNIHandleBlock " INTPTR_FORMAT " blocks=%d used=%d free=%d add=%d", | |
519 this, blocks, total-free, free, _allocate_before_rebuild); | |
520 } | |
521 } | |
522 | |
523 | |
524 bool JNIHandleBlock::contains(jobject handle) const { | |
525 return ((jobject)&_handles[0] <= handle && handle<(jobject)&_handles[_top]); | |
526 } | |
527 | |
528 | |
529 bool JNIHandleBlock::chain_contains(jobject handle) const { | |
530 for (JNIHandleBlock* current = (JNIHandleBlock*) this; current != NULL; current = current->_next) { | |
531 if (current->contains(handle)) { | |
532 return true; | |
533 } | |
534 } | |
535 return false; | |
536 } | |
537 | |
538 | |
539 int JNIHandleBlock::length() const { | |
540 int result = 1; | |
541 for (JNIHandleBlock* current = _next; current != NULL; current = current->_next) { | |
542 result++; | |
543 } | |
544 return result; | |
545 } | |
546 | |
22877
d68158e12cea
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
poonam
parents:
17937
diff
changeset
|
547 const size_t JNIHandleBlock::get_number_of_live_handles() { |
d68158e12cea
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
poonam
parents:
17937
diff
changeset
|
548 CountHandleClosure counter; |
d68158e12cea
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
poonam
parents:
17937
diff
changeset
|
549 oops_do(&counter); |
d68158e12cea
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
poonam
parents:
17937
diff
changeset
|
550 return counter.count(); |
d68158e12cea
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
poonam
parents:
17937
diff
changeset
|
551 } |
d68158e12cea
8043224: -Xcheck:jni improvements to exception checking and excessive local refs
poonam
parents:
17937
diff
changeset
|
552 |
0 | 553 // This method is not thread-safe, i.e., must be called whule holding a lock on the |
554 // structure. | |
555 long JNIHandleBlock::memory_usage() const { | |
556 return length() * sizeof(JNIHandleBlock); | |
557 } | |
558 | |
559 | |
560 #ifndef PRODUCT | |
561 | |
562 bool JNIHandleBlock::any_contains(jobject handle) { | |
563 for (JNIHandleBlock* current = _block_list; current != NULL; current = current->_block_list_link) { | |
564 if (current->contains(handle)) { | |
565 return true; | |
566 } | |
567 } | |
568 return false; | |
569 } | |
570 | |
571 void JNIHandleBlock::print_statistics() { | |
572 int used_blocks = 0; | |
573 int free_blocks = 0; | |
574 int used_handles = 0; | |
575 int free_handles = 0; | |
576 JNIHandleBlock* block = _block_list; | |
577 while (block != NULL) { | |
578 if (block->_top > 0) { | |
579 used_blocks++; | |
580 } else { | |
581 free_blocks++; | |
582 } | |
583 used_handles += block->_top; | |
584 free_handles += (block_size_in_oops - block->_top); | |
585 block = block->_block_list_link; | |
586 } | |
587 tty->print_cr("JNIHandleBlocks statistics"); | |
588 tty->print_cr("- blocks allocated: %d", used_blocks + free_blocks); | |
589 tty->print_cr("- blocks in use: %d", used_blocks); | |
590 tty->print_cr("- blocks free: %d", free_blocks); | |
591 tty->print_cr("- handles in use: %d", used_handles); | |
592 tty->print_cr("- handles free: %d", free_handles); | |
593 } | |
594 | |
595 #endif |