duke@435: /* coleenp@4037: * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" stefank@2314: #include "oops/oop.inline.hpp" kamg@2467: #include "prims/jvmtiExport.hpp" stefank@2314: #include "runtime/jniHandles.hpp" stefank@2314: #include "runtime/mutexLocker.hpp" stefank@4299: #include "runtime/thread.inline.hpp" duke@435: duke@435: duke@435: JNIHandleBlock* JNIHandles::_global_handles = NULL; duke@435: JNIHandleBlock* JNIHandles::_weak_global_handles = NULL; duke@435: oop JNIHandles::_deleted_handle = NULL; duke@435: duke@435: duke@435: jobject JNIHandles::make_local(oop obj) { duke@435: if (obj == NULL) { duke@435: return NULL; // ignore null handles duke@435: } else { duke@435: Thread* thread = Thread::current(); duke@435: assert(Universe::heap()->is_in_reserved(obj), "sanity check"); duke@435: return thread->active_handles()->allocate_handle(obj); duke@435: } duke@435: } duke@435: duke@435: duke@435: // optimized versions duke@435: duke@435: jobject JNIHandles::make_local(Thread* thread, oop obj) { duke@435: if (obj == NULL) { duke@435: return NULL; // ignore null handles duke@435: } else { duke@435: assert(Universe::heap()->is_in_reserved(obj), "sanity check"); duke@435: return thread->active_handles()->allocate_handle(obj); duke@435: } duke@435: } duke@435: duke@435: duke@435: jobject JNIHandles::make_local(JNIEnv* env, oop obj) { duke@435: if (obj == NULL) { duke@435: return NULL; // ignore null handles duke@435: } else { duke@435: JavaThread* thread = JavaThread::thread_from_jni_environment(env); duke@435: assert(Universe::heap()->is_in_reserved(obj), "sanity check"); duke@435: return thread->active_handles()->allocate_handle(obj); duke@435: } duke@435: } duke@435: duke@435: duke@435: jobject JNIHandles::make_global(Handle obj) { never@1971: assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); duke@435: jobject res = NULL; duke@435: if (!obj.is_null()) { duke@435: // ignore null handles duke@435: MutexLocker ml(JNIGlobalHandle_lock); duke@435: assert(Universe::heap()->is_in_reserved(obj()), "sanity check"); duke@435: res = _global_handles->allocate_handle(obj()); duke@435: } else { duke@435: CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); duke@435: } duke@435: duke@435: return res; duke@435: } duke@435: duke@435: duke@435: jobject JNIHandles::make_weak_global(Handle obj) { never@1971: assert(!Universe::heap()->is_gc_active(), "can't extend the root set during GC"); duke@435: jobject res = NULL; duke@435: if (!obj.is_null()) { duke@435: // ignore null handles duke@435: MutexLocker ml(JNIGlobalHandle_lock); duke@435: assert(Universe::heap()->is_in_reserved(obj()), "sanity check"); duke@435: res = _weak_global_handles->allocate_handle(obj()); duke@435: } else { duke@435: CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops()); duke@435: } duke@435: return res; duke@435: } duke@435: duke@435: duke@435: void JNIHandles::destroy_global(jobject handle) { duke@435: if (handle != NULL) { duke@435: assert(is_global_handle(handle), "Invalid delete of global JNI handle"); duke@435: *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it duke@435: } duke@435: } duke@435: duke@435: duke@435: void JNIHandles::destroy_weak_global(jobject handle) { duke@435: if (handle != NULL) { duke@435: assert(!CheckJNICalls || is_weak_global_handle(handle), "Invalid delete of weak global JNI handle"); duke@435: *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it duke@435: } duke@435: } duke@435: duke@435: duke@435: void JNIHandles::oops_do(OopClosure* f) { duke@435: f->do_oop(&_deleted_handle); duke@435: _global_handles->oops_do(f); duke@435: } duke@435: duke@435: duke@435: void JNIHandles::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f) { duke@435: _weak_global_handles->weak_oops_do(is_alive, f); duke@435: } duke@435: duke@435: duke@435: void JNIHandles::initialize() { duke@435: _global_handles = JNIHandleBlock::allocate_block(); duke@435: _weak_global_handles = JNIHandleBlock::allocate_block(); duke@435: EXCEPTION_MARK; duke@435: // We will never reach the CATCH below since Exceptions::_throw will cause duke@435: // the VM to exit if an exception is thrown during initialization coleenp@4037: Klass* k = SystemDictionary::Object_klass(); coleenp@4037: _deleted_handle = InstanceKlass::cast(k)->allocate_instance(CATCH); duke@435: } duke@435: duke@435: duke@435: bool JNIHandles::is_local_handle(Thread* thread, jobject handle) { duke@435: JNIHandleBlock* block = thread->active_handles(); duke@435: duke@435: // Look back past possible native calls to jni_PushLocalFrame. duke@435: while (block != NULL) { duke@435: if (block->chain_contains(handle)) { duke@435: return true; duke@435: } duke@435: block = block->pop_frame_link(); duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: duke@435: // Determine if the handle is somewhere in the current thread's stack. duke@435: // We easily can't isolate any particular stack frame the handle might duke@435: // come from, so we'll check the whole stack. duke@435: duke@435: bool JNIHandles::is_frame_handle(JavaThread* thr, jobject obj) { duke@435: // If there is no java frame, then this must be top level code, such duke@435: // as the java command executable, in which case, this type of handle duke@435: // is not permitted. duke@435: return (thr->has_last_Java_frame() && duke@435: (void*)obj < (void*)thr->stack_base() && duke@435: (void*)obj >= (void*)thr->last_Java_sp()); duke@435: } duke@435: duke@435: duke@435: bool JNIHandles::is_global_handle(jobject handle) { duke@435: return _global_handles->chain_contains(handle); duke@435: } duke@435: duke@435: duke@435: bool JNIHandles::is_weak_global_handle(jobject handle) { duke@435: return _weak_global_handles->chain_contains(handle); duke@435: } duke@435: duke@435: long JNIHandles::global_handle_memory_usage() { duke@435: return _global_handles->memory_usage(); duke@435: } duke@435: duke@435: long JNIHandles::weak_global_handle_memory_usage() { duke@435: return _weak_global_handles->memory_usage(); duke@435: } duke@435: duke@435: duke@435: class AlwaysAliveClosure: public BoolObjectClosure { duke@435: public: duke@435: bool do_object_b(oop obj) { return true; } duke@435: }; duke@435: duke@435: class CountHandleClosure: public OopClosure { duke@435: private: duke@435: int _count; duke@435: public: duke@435: CountHandleClosure(): _count(0) {} coleenp@548: virtual void do_oop(oop* unused) { duke@435: _count++; duke@435: } coleenp@548: virtual void do_oop(narrowOop* unused) { ShouldNotReachHere(); } duke@435: int count() { return _count; } duke@435: }; duke@435: duke@435: // We assume this is called at a safepoint: no lock is needed. duke@435: void JNIHandles::print_on(outputStream* st) { duke@435: assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); duke@435: assert(_global_handles != NULL && _weak_global_handles != NULL, duke@435: "JNIHandles not initialized"); duke@435: duke@435: CountHandleClosure global_handle_count; duke@435: AlwaysAliveClosure always_alive; duke@435: oops_do(&global_handle_count); duke@435: weak_oops_do(&always_alive, &global_handle_count); duke@435: duke@435: st->print_cr("JNI global references: %d", global_handle_count.count()); duke@435: st->cr(); duke@435: st->flush(); duke@435: } duke@435: duke@435: class VerifyHandleClosure: public OopClosure { duke@435: public: coleenp@548: virtual void do_oop(oop* root) { duke@435: (*root)->verify(); duke@435: } coleenp@548: virtual void do_oop(narrowOop* root) { ShouldNotReachHere(); } duke@435: }; duke@435: duke@435: void JNIHandles::verify() { duke@435: VerifyHandleClosure verify_handle; duke@435: AlwaysAliveClosure always_alive; duke@435: duke@435: oops_do(&verify_handle); duke@435: weak_oops_do(&always_alive, &verify_handle); duke@435: } duke@435: duke@435: duke@435: duke@435: void jni_handles_init() { duke@435: JNIHandles::initialize(); duke@435: } duke@435: duke@435: duke@435: int JNIHandleBlock::_blocks_allocated = 0; duke@435: JNIHandleBlock* JNIHandleBlock::_block_free_list = NULL; duke@435: #ifndef PRODUCT duke@435: JNIHandleBlock* JNIHandleBlock::_block_list = NULL; duke@435: #endif duke@435: duke@435: duke@435: void JNIHandleBlock::zap() { duke@435: // Zap block values duke@435: _top = 0; duke@435: for (int index = 0; index < block_size_in_oops; index++) { duke@435: _handles[index] = badJNIHandle; duke@435: } duke@435: } duke@435: duke@435: JNIHandleBlock* JNIHandleBlock::allocate_block(Thread* thread) { duke@435: assert(thread == NULL || thread == Thread::current(), "sanity check"); duke@435: JNIHandleBlock* block; duke@435: // Check the thread-local free list for a block so we don't duke@435: // have to acquire a mutex. duke@435: if (thread != NULL && thread->free_handle_block() != NULL) { duke@435: block = thread->free_handle_block(); duke@435: thread->set_free_handle_block(block->_next); duke@435: } duke@435: else { duke@435: // locking with safepoint checking introduces a potential deadlock: duke@435: // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock duke@435: // - another would hold Threads_lock (jni_AttachCurrentThread) and then duke@435: // JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block) duke@435: MutexLockerEx ml(JNIHandleBlockFreeList_lock, duke@435: Mutex::_no_safepoint_check_flag); duke@435: if (_block_free_list == NULL) { duke@435: // Allocate new block duke@435: block = new JNIHandleBlock(); duke@435: _blocks_allocated++; duke@435: if (TraceJNIHandleAllocation) { duke@435: tty->print_cr("JNIHandleBlock " INTPTR_FORMAT " allocated (%d total blocks)", duke@435: block, _blocks_allocated); duke@435: } duke@435: if (ZapJNIHandleArea) block->zap(); duke@435: #ifndef PRODUCT duke@435: // Link new block to list of all allocated blocks duke@435: block->_block_list_link = _block_list; duke@435: _block_list = block; duke@435: #endif duke@435: } else { duke@435: // Get block from free list duke@435: block = _block_free_list; duke@435: _block_free_list = _block_free_list->_next; duke@435: } duke@435: } duke@435: block->_top = 0; duke@435: block->_next = NULL; duke@435: block->_pop_frame_link = NULL; duke@435: // _last, _free_list & _allocate_before_rebuild initialized in allocate_handle duke@435: debug_only(block->_last = NULL); duke@435: debug_only(block->_free_list = NULL); duke@435: debug_only(block->_allocate_before_rebuild = -1); duke@435: return block; duke@435: } duke@435: duke@435: duke@435: void JNIHandleBlock::release_block(JNIHandleBlock* block, Thread* thread) { duke@435: assert(thread == NULL || thread == Thread::current(), "sanity check"); duke@435: JNIHandleBlock* pop_frame_link = block->pop_frame_link(); duke@435: // Put returned block at the beginning of the thread-local free list. duke@435: // Note that if thread == NULL, we use it as an implicit argument that duke@435: // we _don't_ want the block to be kept on the free_handle_block. duke@435: // See for instance JavaThread::exit(). duke@435: if (thread != NULL ) { duke@435: if (ZapJNIHandleArea) block->zap(); duke@435: JNIHandleBlock* freelist = thread->free_handle_block(); duke@435: block->_pop_frame_link = NULL; duke@435: thread->set_free_handle_block(block); duke@435: duke@435: // Add original freelist to end of chain duke@435: if ( freelist != NULL ) { duke@435: while ( block->_next != NULL ) block = block->_next; duke@435: block->_next = freelist; duke@435: } duke@435: block = NULL; duke@435: } duke@435: if (block != NULL) { duke@435: // Return blocks to free list duke@435: // locking with safepoint checking introduces a potential deadlock: duke@435: // - we would hold JNIHandleBlockFreeList_lock and then Threads_lock duke@435: // - another would hold Threads_lock (jni_AttachCurrentThread) and then duke@435: // JNIHandleBlockFreeList_lock (JNIHandleBlock::allocate_block) duke@435: MutexLockerEx ml(JNIHandleBlockFreeList_lock, duke@435: Mutex::_no_safepoint_check_flag); duke@435: while (block != NULL) { duke@435: if (ZapJNIHandleArea) block->zap(); duke@435: JNIHandleBlock* next = block->_next; duke@435: block->_next = _block_free_list; duke@435: _block_free_list = block; duke@435: block = next; duke@435: } duke@435: } duke@435: if (pop_frame_link != NULL) { duke@435: // As a sanity check we release blocks pointed to by the pop_frame_link. duke@435: // This should never happen (only if PopLocalFrame is not called the duke@435: // correct number of times). duke@435: release_block(pop_frame_link, thread); duke@435: } duke@435: } duke@435: duke@435: duke@435: void JNIHandleBlock::oops_do(OopClosure* f) { duke@435: JNIHandleBlock* current_chain = this; duke@435: // Iterate over chain of blocks, followed by chains linked through the duke@435: // pop frame links. duke@435: while (current_chain != NULL) { duke@435: for (JNIHandleBlock* current = current_chain; current != NULL; duke@435: current = current->_next) { duke@435: assert(current == current_chain || current->pop_frame_link() == NULL, duke@435: "only blocks first in chain should have pop frame link set"); duke@435: for (int index = 0; index < current->_top; index++) { duke@435: oop* root = &(current->_handles)[index]; duke@435: oop value = *root; duke@435: // traverse heap pointers only, not deleted handles or free list duke@435: // pointers duke@435: if (value != NULL && Universe::heap()->is_in_reserved(value)) { duke@435: f->do_oop(root); duke@435: } duke@435: } duke@435: // the next handle block is valid only if current block is full duke@435: if (current->_top < block_size_in_oops) { duke@435: break; duke@435: } duke@435: } duke@435: current_chain = current_chain->pop_frame_link(); duke@435: } duke@435: } duke@435: duke@435: duke@435: void JNIHandleBlock::weak_oops_do(BoolObjectClosure* is_alive, duke@435: OopClosure* f) { duke@435: for (JNIHandleBlock* current = this; current != NULL; current = current->_next) { duke@435: assert(current->pop_frame_link() == NULL, duke@435: "blocks holding weak global JNI handles should not have pop frame link set"); duke@435: for (int index = 0; index < current->_top; index++) { duke@435: oop* root = &(current->_handles)[index]; duke@435: oop value = *root; duke@435: // traverse heap pointers only, not deleted handles or free list pointers duke@435: if (value != NULL && Universe::heap()->is_in_reserved(value)) { duke@435: if (is_alive->do_object_b(value)) { duke@435: // The weakly referenced object is alive, update pointer duke@435: f->do_oop(root); duke@435: } else { duke@435: // The weakly referenced object is not alive, clear the reference by storing NULL duke@435: if (TraceReferenceGC) { duke@435: tty->print_cr("Clearing JNI weak reference (" INTPTR_FORMAT ")", root); duke@435: } duke@435: *root = NULL; duke@435: } duke@435: } duke@435: } duke@435: // the next handle block is valid only if current block is full duke@435: if (current->_top < block_size_in_oops) { duke@435: break; duke@435: } duke@435: } kamg@2445: kamg@2445: /* kamg@2467: * JVMTI data structures may also contain weak oops. The iteration of them kamg@2467: * is placed here so that we don't need to add it to each of the collectors. kamg@2445: */ kamg@2467: JvmtiExport::weak_oops_do(is_alive, f); duke@435: } duke@435: duke@435: duke@435: jobject JNIHandleBlock::allocate_handle(oop obj) { duke@435: assert(Universe::heap()->is_in_reserved(obj), "sanity check"); duke@435: if (_top == 0) { duke@435: // This is the first allocation or the initial block got zapped when duke@435: // entering a native function. If we have any following blocks they are duke@435: // not valid anymore. duke@435: for (JNIHandleBlock* current = _next; current != NULL; duke@435: current = current->_next) { duke@435: assert(current->_last == NULL, "only first block should have _last set"); duke@435: assert(current->_free_list == NULL, duke@435: "only first block should have _free_list set"); duke@435: current->_top = 0; duke@435: if (ZapJNIHandleArea) current->zap(); duke@435: } duke@435: // Clear initial block duke@435: _free_list = NULL; duke@435: _allocate_before_rebuild = 0; duke@435: _last = this; duke@435: if (ZapJNIHandleArea) zap(); duke@435: } duke@435: duke@435: // Try last block duke@435: if (_last->_top < block_size_in_oops) { duke@435: oop* handle = &(_last->_handles)[_last->_top++]; duke@435: *handle = obj; duke@435: return (jobject) handle; duke@435: } duke@435: duke@435: // Try free list duke@435: if (_free_list != NULL) { duke@435: oop* handle = _free_list; duke@435: _free_list = (oop*) *_free_list; duke@435: *handle = obj; duke@435: return (jobject) handle; duke@435: } duke@435: // Check if unused block follow last duke@435: if (_last->_next != NULL) { duke@435: // update last and retry duke@435: _last = _last->_next; duke@435: return allocate_handle(obj); duke@435: } duke@435: duke@435: // No space available, we have to rebuild free list or expand duke@435: if (_allocate_before_rebuild == 0) { duke@435: rebuild_free_list(); // updates _allocate_before_rebuild counter duke@435: } else { duke@435: // Append new block duke@435: Thread* thread = Thread::current(); duke@435: Handle obj_handle(thread, obj); duke@435: // This can block, so we need to preserve obj accross call. duke@435: _last->_next = JNIHandleBlock::allocate_block(thread); duke@435: _last = _last->_next; duke@435: _allocate_before_rebuild--; duke@435: obj = obj_handle(); duke@435: } duke@435: return allocate_handle(obj); // retry duke@435: } duke@435: duke@435: duke@435: void JNIHandleBlock::rebuild_free_list() { duke@435: assert(_allocate_before_rebuild == 0 && _free_list == NULL, "just checking"); duke@435: int free = 0; duke@435: int blocks = 0; duke@435: for (JNIHandleBlock* current = this; current != NULL; current = current->_next) { duke@435: for (int index = 0; index < current->_top; index++) { duke@435: oop* handle = &(current->_handles)[index]; duke@435: if (*handle == JNIHandles::deleted_handle()) { duke@435: // this handle was cleared out by a delete call, reuse it duke@435: *handle = (oop) _free_list; duke@435: _free_list = handle; duke@435: free++; duke@435: } duke@435: } duke@435: // we should not rebuild free list if there are unused handles at the end duke@435: assert(current->_top == block_size_in_oops, "just checking"); duke@435: blocks++; duke@435: } duke@435: // Heuristic: if more than half of the handles are free we rebuild next time duke@435: // as well, otherwise we append a corresponding number of new blocks before duke@435: // attempting a free list rebuild again. duke@435: int total = blocks * block_size_in_oops; duke@435: int extra = total - 2*free; duke@435: if (extra > 0) { duke@435: // Not as many free handles as we would like - compute number of new blocks to append duke@435: _allocate_before_rebuild = (extra + block_size_in_oops - 1) / block_size_in_oops; duke@435: } duke@435: if (TraceJNIHandleAllocation) { duke@435: tty->print_cr("Rebuild free list JNIHandleBlock " INTPTR_FORMAT " blocks=%d used=%d free=%d add=%d", duke@435: this, blocks, total-free, free, _allocate_before_rebuild); duke@435: } duke@435: } duke@435: duke@435: duke@435: bool JNIHandleBlock::contains(jobject handle) const { duke@435: return ((jobject)&_handles[0] <= handle && handle<(jobject)&_handles[_top]); duke@435: } duke@435: duke@435: duke@435: bool JNIHandleBlock::chain_contains(jobject handle) const { duke@435: for (JNIHandleBlock* current = (JNIHandleBlock*) this; current != NULL; current = current->_next) { duke@435: if (current->contains(handle)) { duke@435: return true; duke@435: } duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: duke@435: int JNIHandleBlock::length() const { duke@435: int result = 1; duke@435: for (JNIHandleBlock* current = _next; current != NULL; current = current->_next) { duke@435: result++; duke@435: } duke@435: return result; duke@435: } duke@435: duke@435: // This method is not thread-safe, i.e., must be called whule holding a lock on the duke@435: // structure. duke@435: long JNIHandleBlock::memory_usage() const { duke@435: return length() * sizeof(JNIHandleBlock); duke@435: } duke@435: duke@435: duke@435: #ifndef PRODUCT duke@435: duke@435: bool JNIHandleBlock::any_contains(jobject handle) { duke@435: for (JNIHandleBlock* current = _block_list; current != NULL; current = current->_block_list_link) { duke@435: if (current->contains(handle)) { duke@435: return true; duke@435: } duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: void JNIHandleBlock::print_statistics() { duke@435: int used_blocks = 0; duke@435: int free_blocks = 0; duke@435: int used_handles = 0; duke@435: int free_handles = 0; duke@435: JNIHandleBlock* block = _block_list; duke@435: while (block != NULL) { duke@435: if (block->_top > 0) { duke@435: used_blocks++; duke@435: } else { duke@435: free_blocks++; duke@435: } duke@435: used_handles += block->_top; duke@435: free_handles += (block_size_in_oops - block->_top); duke@435: block = block->_block_list_link; duke@435: } duke@435: tty->print_cr("JNIHandleBlocks statistics"); duke@435: tty->print_cr("- blocks allocated: %d", used_blocks + free_blocks); duke@435: tty->print_cr("- blocks in use: %d", used_blocks); duke@435: tty->print_cr("- blocks free: %d", free_blocks); duke@435: tty->print_cr("- handles in use: %d", used_handles); duke@435: tty->print_cr("- handles free: %d", free_handles); duke@435: } duke@435: duke@435: #endif