src/share/vm/runtime/jniHandles.cpp

Thu, 24 May 2018 20:03:11 +0800

author
aoqi
date
Thu, 24 May 2018 20:03:11 +0800
changeset 8868
91ddc23482a4
parent 7994
04ff2f6cd0eb
child 9041
95a08233f46c
permissions
-rw-r--r--

Increase MaxHeapSize for better performance on MIPS

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

mercurial