aoqi@0: /* kevinw@8395: * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@1: /* aoqi@1: * This file has been modified by Loongson Technology in 2015. These aoqi@1: * modifications are Copyright (c) 2015 Loongson Technology, and are made aoqi@1: * available on the same license terms set forth above. aoqi@1: */ aoqi@1: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "classfile/systemDictionary.hpp" aoqi@0: #include "code/debugInfoRec.hpp" aoqi@0: #include "code/nmethod.hpp" aoqi@0: #include "code/pcDesc.hpp" aoqi@0: #include "code/scopeDesc.hpp" aoqi@0: #include "interpreter/bytecode.hpp" aoqi@0: #include "interpreter/interpreter.hpp" aoqi@0: #include "interpreter/oopMapCache.hpp" aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "memory/oopFactory.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "oops/method.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "prims/jvmtiThreadState.hpp" aoqi@0: #include "runtime/biasedLocking.hpp" aoqi@0: #include "runtime/compilationPolicy.hpp" aoqi@0: #include "runtime/deoptimization.hpp" aoqi@0: #include "runtime/interfaceSupport.hpp" aoqi@0: #include "runtime/sharedRuntime.hpp" aoqi@0: #include "runtime/signature.hpp" aoqi@0: #include "runtime/stubRoutines.hpp" aoqi@0: #include "runtime/thread.hpp" aoqi@0: #include "runtime/vframe.hpp" aoqi@0: #include "runtime/vframeArray.hpp" aoqi@0: #include "runtime/vframe_hp.hpp" aoqi@0: #include "utilities/events.hpp" aoqi@0: #include "utilities/xmlstream.hpp" aoqi@0: #ifdef TARGET_ARCH_x86 aoqi@0: # include "vmreg_x86.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_sparc aoqi@0: # include "vmreg_sparc.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_zero aoqi@0: # include "vmreg_zero.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_arm aoqi@0: # include "vmreg_arm.inline.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_ppc aoqi@0: # include "vmreg_ppc.inline.hpp" aoqi@0: #endif aoqi@1: #ifdef TARGET_ARCH_mips aoqi@1: # include "vmreg_mips.inline.hpp" aoqi@1: #endif aoqi@0: #ifdef COMPILER2 dlong@7598: #if defined AD_MD_HPP dlong@7598: # include AD_MD_HPP dlong@7598: #elif defined TARGET_ARCH_MODEL_x86_32 aoqi@0: # include "adfiles/ad_x86_32.hpp" dlong@7598: #elif defined TARGET_ARCH_MODEL_x86_64 aoqi@0: # include "adfiles/ad_x86_64.hpp" dlong@7598: #elif defined TARGET_ARCH_MODEL_sparc aoqi@0: # include "adfiles/ad_sparc.hpp" dlong@7598: #elif defined TARGET_ARCH_MODEL_zero aoqi@0: # include "adfiles/ad_zero.hpp" dlong@7598: #elif defined TARGET_ARCH_MODEL_ppc_64 aoqi@0: # include "adfiles/ad_ppc_64.hpp" aoqi@0: #endif aoqi@1: #ifdef TARGET_ARCH_MODEL_mips_64 aoqi@1: # include "adfiles/ad_mips_64.hpp" aoqi@1: #endif aoqi@0: #endif // COMPILER2 aoqi@0: aoqi@0: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC aoqi@0: aoqi@0: bool DeoptimizationMarker::_is_active = false; aoqi@0: aoqi@0: Deoptimization::UnrollBlock::UnrollBlock(int size_of_deoptimized_frame, aoqi@0: int caller_adjustment, aoqi@0: int caller_actual_parameters, aoqi@0: int number_of_frames, aoqi@0: intptr_t* frame_sizes, aoqi@0: address* frame_pcs, aoqi@0: BasicType return_type) { aoqi@0: _size_of_deoptimized_frame = size_of_deoptimized_frame; aoqi@0: _caller_adjustment = caller_adjustment; aoqi@0: _caller_actual_parameters = caller_actual_parameters; aoqi@0: _number_of_frames = number_of_frames; aoqi@0: _frame_sizes = frame_sizes; aoqi@0: _frame_pcs = frame_pcs; aoqi@0: _register_block = NEW_C_HEAP_ARRAY(intptr_t, RegisterMap::reg_count * 2, mtCompiler); aoqi@0: _return_type = return_type; aoqi@0: _initial_info = 0; aoqi@0: // PD (x86 only) aoqi@0: _counter_temp = 0; aoqi@0: _unpack_kind = 0; aoqi@0: _sender_sp_temp = 0; aoqi@0: aoqi@0: _total_frame_sizes = size_of_frames(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: Deoptimization::UnrollBlock::~UnrollBlock() { aoqi@0: FREE_C_HEAP_ARRAY(intptr_t, _frame_sizes, mtCompiler); aoqi@0: FREE_C_HEAP_ARRAY(intptr_t, _frame_pcs, mtCompiler); aoqi@0: FREE_C_HEAP_ARRAY(intptr_t, _register_block, mtCompiler); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: intptr_t* Deoptimization::UnrollBlock::value_addr_at(int register_number) const { aoqi@0: assert(register_number < RegisterMap::reg_count, "checking register number"); aoqi@0: return &_register_block[register_number * 2]; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: aoqi@0: int Deoptimization::UnrollBlock::size_of_frames() const { aoqi@0: // Acount first for the adjustment of the initial frame aoqi@0: int result = _caller_adjustment; aoqi@0: for (int index = 0; index < number_of_frames(); index++) { aoqi@0: result += frame_sizes()[index]; aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void Deoptimization::UnrollBlock::print() { aoqi@0: ttyLocker ttyl; aoqi@0: tty->print_cr("UnrollBlock"); aoqi@0: tty->print_cr(" size_of_deoptimized_frame = %d", _size_of_deoptimized_frame); aoqi@0: tty->print( " frame_sizes: "); aoqi@0: for (int index = 0; index < number_of_frames(); index++) { aoqi@0: tty->print("%d ", frame_sizes()[index]); aoqi@0: } aoqi@0: tty->cr(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // In order to make fetch_unroll_info work properly with escape aoqi@0: // analysis, The method was changed from JRT_LEAF to JRT_BLOCK_ENTRY and aoqi@0: // ResetNoHandleMark and HandleMark were removed from it. The actual reallocation aoqi@0: // of previously eliminated objects occurs in realloc_objects, which is aoqi@0: // called from the method fetch_unroll_info_helper below. aoqi@0: JRT_BLOCK_ENTRY(Deoptimization::UnrollBlock*, Deoptimization::fetch_unroll_info(JavaThread* thread)) aoqi@0: // It is actually ok to allocate handles in a leaf method. It causes no safepoints, aoqi@0: // but makes the entry a little slower. There is however a little dance we have to aoqi@0: // do in debug mode to get around the NoHandleMark code in the JRT_LEAF macro aoqi@0: aoqi@0: // fetch_unroll_info() is called at the beginning of the deoptimization aoqi@0: // handler. Note this fact before we start generating temporary frames aoqi@0: // that can confuse an asynchronous stack walker. This counter is aoqi@0: // decremented at the end of unpack_frames(). aoqi@0: thread->inc_in_deopt_handler(); aoqi@0: aoqi@0: return fetch_unroll_info_helper(thread); aoqi@0: JRT_END aoqi@0: aoqi@0: aoqi@0: // This is factored, since it is both called from a JRT_LEAF (deoptimization) and a JRT_ENTRY (uncommon_trap) aoqi@0: Deoptimization::UnrollBlock* Deoptimization::fetch_unroll_info_helper(JavaThread* thread) { aoqi@0: aoqi@0: // Note: there is a safepoint safety issue here. No matter whether we enter aoqi@0: // via vanilla deopt or uncommon trap we MUST NOT stop at a safepoint once aoqi@0: // the vframeArray is created. aoqi@0: // aoqi@0: aoqi@0: // Allocate our special deoptimization ResourceMark aoqi@0: DeoptResourceMark* dmark = new DeoptResourceMark(thread); aoqi@0: assert(thread->deopt_mark() == NULL, "Pending deopt!"); aoqi@0: thread->set_deopt_mark(dmark); aoqi@0: aoqi@0: frame stub_frame = thread->last_frame(); // Makes stack walkable as side effect aoqi@0: RegisterMap map(thread, true); aoqi@0: RegisterMap dummy_map(thread, false); aoqi@0: // Now get the deoptee with a valid map aoqi@0: frame deoptee = stub_frame.sender(&map); aoqi@0: // Set the deoptee nmethod aoqi@0: assert(thread->deopt_nmethod() == NULL, "Pending deopt!"); aoqi@0: thread->set_deopt_nmethod(deoptee.cb()->as_nmethod_or_null()); aoqi@0: aoqi@0: if (VerifyStack) { aoqi@0: thread->validate_frame_layout(); aoqi@0: } aoqi@0: aoqi@0: // Create a growable array of VFrames where each VFrame represents an inlined aoqi@0: // Java frame. This storage is allocated with the usual system arena. aoqi@0: assert(deoptee.is_compiled_frame(), "Wrong frame type"); aoqi@0: GrowableArray* chunk = new GrowableArray(10); aoqi@0: vframe* vf = vframe::new_vframe(&deoptee, &map, thread); aoqi@0: while (!vf->is_top()) { aoqi@0: assert(vf->is_compiled_frame(), "Wrong frame type"); aoqi@0: chunk->push(compiledVFrame::cast(vf)); aoqi@0: vf = vf->sender(); aoqi@0: } aoqi@0: assert(vf->is_compiled_frame(), "Wrong frame type"); aoqi@0: chunk->push(compiledVFrame::cast(vf)); aoqi@0: roland@7419: bool realloc_failures = false; roland@7419: aoqi@0: #ifdef COMPILER2 aoqi@0: // Reallocate the non-escaping objects and restore their fields. Then aoqi@0: // relock objects if synchronization on them was eliminated. aoqi@0: if (DoEscapeAnalysis || EliminateNestedLocks) { aoqi@0: if (EliminateAllocations) { aoqi@0: assert (chunk->at(0)->scope() != NULL,"expect only compiled java frames"); aoqi@0: GrowableArray* objects = chunk->at(0)->scope()->objects(); aoqi@0: aoqi@0: // The flag return_oop() indicates call sites which return oop aoqi@0: // in compiled code. Such sites include java method calls, aoqi@0: // runtime calls (for example, used to allocate new objects/arrays aoqi@0: // on slow code path) and any other calls generated in compiled code. aoqi@0: // It is not guaranteed that we can get such information here only aoqi@0: // by analyzing bytecode in deoptimized frames. This is why this flag aoqi@0: // is set during method compilation (see Compile::Process_OopMap_Node()). roland@7168: // If the previous frame was popped, we don't have a result. roland@7168: bool save_oop_result = chunk->at(0)->scope()->return_oop() && !thread->popframe_forcing_deopt_reexecution(); aoqi@0: Handle return_value; aoqi@0: if (save_oop_result) { aoqi@0: // Reallocation may trigger GC. If deoptimization happened on return from aoqi@0: // call which returns oop we need to save it since it is not in oopmap. aoqi@0: oop result = deoptee.saved_oop_result(&map); aoqi@0: assert(result == NULL || result->is_oop(), "must be oop"); aoqi@0: return_value = Handle(thread, result); aoqi@0: assert(Universe::heap()->is_in_or_null(result), "must be heap pointer"); aoqi@0: if (TraceDeoptimization) { aoqi@0: ttyLocker ttyl; aoqi@0: tty->print_cr("SAVED OOP RESULT " INTPTR_FORMAT " in thread " INTPTR_FORMAT, (void *)result, thread); aoqi@0: } aoqi@0: } aoqi@0: if (objects != NULL) { aoqi@0: JRT_BLOCK roland@7419: realloc_failures = realloc_objects(thread, &deoptee, objects, THREAD); aoqi@0: JRT_END roland@7419: reassign_fields(&deoptee, &map, objects, realloc_failures); aoqi@0: #ifndef PRODUCT aoqi@0: if (TraceDeoptimization) { aoqi@0: ttyLocker ttyl; aoqi@0: tty->print_cr("REALLOC OBJECTS in thread " INTPTR_FORMAT, thread); roland@7420: print_objects(objects, realloc_failures); aoqi@0: } aoqi@0: #endif aoqi@0: } aoqi@0: if (save_oop_result) { aoqi@0: // Restore result. aoqi@0: deoptee.set_saved_oop_result(&map, return_value()); aoqi@0: } aoqi@0: } aoqi@0: if (EliminateLocks) { aoqi@0: #ifndef PRODUCT aoqi@0: bool first = true; aoqi@0: #endif aoqi@0: for (int i = 0; i < chunk->length(); i++) { aoqi@0: compiledVFrame* cvf = chunk->at(i); aoqi@0: assert (cvf->scope() != NULL,"expect only compiled java frames"); aoqi@0: GrowableArray* monitors = cvf->monitors(); aoqi@0: if (monitors->is_nonempty()) { roland@7419: relock_objects(monitors, thread, realloc_failures); aoqi@0: #ifndef PRODUCT aoqi@0: if (TraceDeoptimization) { aoqi@0: ttyLocker ttyl; aoqi@0: for (int j = 0; j < monitors->length(); j++) { aoqi@0: MonitorInfo* mi = monitors->at(j); aoqi@0: if (mi->eliminated()) { aoqi@0: if (first) { aoqi@0: first = false; aoqi@0: tty->print_cr("RELOCK OBJECTS in thread " INTPTR_FORMAT, thread); aoqi@0: } roland@7419: if (mi->owner_is_scalar_replaced()) { roland@7419: Klass* k = java_lang_Class::as_Klass(mi->owner_klass()); roland@7419: tty->print_cr(" failed reallocation for klass %s", k->external_name()); roland@7419: } else { roland@7419: tty->print_cr(" object <" INTPTR_FORMAT "> locked", (void *)mi->owner()); roland@7419: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: #endif aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: #endif // COMPILER2 aoqi@0: // Ensure that no safepoint is taken after pointers have been stored aoqi@0: // in fields of rematerialized objects. If a safepoint occurs from here on aoqi@0: // out the java state residing in the vframeArray will be missed. aoqi@0: No_Safepoint_Verifier no_safepoint; aoqi@0: roland@7419: vframeArray* array = create_vframeArray(thread, deoptee, &map, chunk, realloc_failures); roland@7419: #ifdef COMPILER2 roland@7419: if (realloc_failures) { roland@7419: pop_frames_failed_reallocs(thread, array); roland@7419: } roland@7419: #endif aoqi@0: roland@7419: assert(thread->vframe_array_head() == NULL, "Pending deopt!"); aoqi@0: thread->set_vframe_array_head(array); aoqi@0: aoqi@0: // Now that the vframeArray has been created if we have any deferred local writes aoqi@0: // added by jvmti then we can free up that structure as the data is now in the aoqi@0: // vframeArray aoqi@0: aoqi@0: if (thread->deferred_locals() != NULL) { aoqi@0: GrowableArray* list = thread->deferred_locals(); aoqi@0: int i = 0; aoqi@0: do { aoqi@0: // Because of inlining we could have multiple vframes for a single frame aoqi@0: // and several of the vframes could have deferred writes. Find them all. aoqi@0: if (list->at(i)->id() == array->original().id()) { aoqi@0: jvmtiDeferredLocalVariableSet* dlv = list->at(i); aoqi@0: list->remove_at(i); aoqi@0: // individual jvmtiDeferredLocalVariableSet are CHeapObj's aoqi@0: delete dlv; aoqi@0: } else { aoqi@0: i++; aoqi@0: } aoqi@0: } while ( i < list->length() ); aoqi@0: if (list->length() == 0) { aoqi@0: thread->set_deferred_locals(NULL); aoqi@0: // free the list and elements back to C heap. aoqi@0: delete list; aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: #ifndef SHARK aoqi@0: // Compute the caller frame based on the sender sp of stub_frame and stored frame sizes info. aoqi@0: CodeBlob* cb = stub_frame.cb(); aoqi@0: // Verify we have the right vframeArray aoqi@0: assert(cb->frame_size() >= 0, "Unexpected frame size"); aoqi@0: intptr_t* unpack_sp = stub_frame.sp() + cb->frame_size(); aoqi@0: aoqi@0: // If the deopt call site is a MethodHandle invoke call site we have aoqi@0: // to adjust the unpack_sp. aoqi@0: nmethod* deoptee_nm = deoptee.cb()->as_nmethod_or_null(); aoqi@0: if (deoptee_nm != NULL && deoptee_nm->is_method_handle_return(deoptee.pc())) aoqi@0: unpack_sp = deoptee.unextended_sp(); aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: assert(cb->is_deoptimization_stub() || cb->is_uncommon_trap_stub(), "just checking"); aoqi@0: #endif aoqi@0: #else aoqi@0: intptr_t* unpack_sp = stub_frame.sender(&dummy_map).unextended_sp(); aoqi@0: #endif // !SHARK aoqi@0: aoqi@0: // This is a guarantee instead of an assert because if vframe doesn't match aoqi@0: // we will unpack the wrong deoptimized frame and wind up in strange places aoqi@0: // where it will be very difficult to figure out what went wrong. Better aoqi@0: // to die an early death here than some very obscure death later when the aoqi@0: // trail is cold. aoqi@0: // Note: on ia64 this guarantee can be fooled by frames with no memory stack aoqi@0: // in that it will fail to detect a problem when there is one. This needs aoqi@0: // more work in tiger timeframe. aoqi@0: guarantee(array->unextended_sp() == unpack_sp, "vframe_array_head must contain the vframeArray to unpack"); aoqi@0: aoqi@0: int number_of_frames = array->frames(); aoqi@0: aoqi@0: // Compute the vframes' sizes. Note that frame_sizes[] entries are ordered from outermost to innermost aoqi@0: // virtual activation, which is the reverse of the elements in the vframes array. aoqi@0: intptr_t* frame_sizes = NEW_C_HEAP_ARRAY(intptr_t, number_of_frames, mtCompiler); aoqi@0: // +1 because we always have an interpreter return address for the final slot. aoqi@0: address* frame_pcs = NEW_C_HEAP_ARRAY(address, number_of_frames + 1, mtCompiler); aoqi@0: int popframe_extra_args = 0; aoqi@0: // Create an interpreter return address for the stub to use as its return aoqi@0: // address so the skeletal frames are perfectly walkable aoqi@0: frame_pcs[number_of_frames] = Interpreter::deopt_entry(vtos, 0); aoqi@0: aoqi@0: // PopFrame requires that the preserved incoming arguments from the recently-popped topmost aoqi@0: // activation be put back on the expression stack of the caller for reexecution aoqi@0: if (JvmtiExport::can_pop_frame() && thread->popframe_forcing_deopt_reexecution()) { aoqi@0: popframe_extra_args = in_words(thread->popframe_preserved_args_size_in_words()); aoqi@0: } aoqi@0: aoqi@0: // Find the current pc for sender of the deoptee. Since the sender may have been deoptimized aoqi@0: // itself since the deoptee vframeArray was created we must get a fresh value of the pc rather aoqi@0: // than simply use array->sender.pc(). This requires us to walk the current set of frames aoqi@0: // aoqi@0: frame deopt_sender = stub_frame.sender(&dummy_map); // First is the deoptee frame aoqi@0: deopt_sender = deopt_sender.sender(&dummy_map); // Now deoptee caller aoqi@0: aoqi@0: // It's possible that the number of paramters at the call site is aoqi@0: // different than number of arguments in the callee when method aoqi@0: // handles are used. If the caller is interpreted get the real aoqi@0: // value so that the proper amount of space can be added to it's aoqi@0: // frame. aoqi@0: bool caller_was_method_handle = false; aoqi@0: if (deopt_sender.is_interpreted_frame()) { aoqi@0: methodHandle method = deopt_sender.interpreter_frame_method(); aoqi@0: Bytecode_invoke cur = Bytecode_invoke_check(method, deopt_sender.interpreter_frame_bci()); aoqi@0: if (cur.is_invokedynamic() || cur.is_invokehandle()) { aoqi@0: // Method handle invokes may involve fairly arbitrary chains of aoqi@0: // calls so it's impossible to know how much actual space the aoqi@0: // caller has for locals. aoqi@0: caller_was_method_handle = true; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // frame_sizes/frame_pcs[0] oldest frame (int or c2i) aoqi@0: // frame_sizes/frame_pcs[1] next oldest frame (int) aoqi@0: // frame_sizes/frame_pcs[n] youngest frame (int) aoqi@0: // aoqi@0: // Now a pc in frame_pcs is actually the return address to the frame's caller (a frame aoqi@0: // owns the space for the return address to it's caller). Confusing ain't it. aoqi@0: // aoqi@0: // The vframe array can address vframes with indices running from aoqi@0: // 0.._frames-1. Index 0 is the youngest frame and _frame - 1 is the oldest (root) frame. aoqi@0: // When we create the skeletal frames we need the oldest frame to be in the zero slot aoqi@0: // in the frame_sizes/frame_pcs so the assembly code can do a trivial walk. aoqi@0: // so things look a little strange in this loop. aoqi@0: // aoqi@0: int callee_parameters = 0; aoqi@0: int callee_locals = 0; aoqi@0: for (int index = 0; index < array->frames(); index++ ) { aoqi@0: // frame[number_of_frames - 1 ] = on_stack_size(youngest) aoqi@0: // frame[number_of_frames - 2 ] = on_stack_size(sender(youngest)) aoqi@0: // frame[number_of_frames - 3 ] = on_stack_size(sender(sender(youngest))) aoqi@0: frame_sizes[number_of_frames - 1 - index] = BytesPerWord * array->element(index)->on_stack_size(callee_parameters, aoqi@0: callee_locals, aoqi@0: index == 0, aoqi@0: popframe_extra_args); aoqi@0: // This pc doesn't have to be perfect just good enough to identify the frame aoqi@0: // as interpreted so the skeleton frame will be walkable aoqi@0: // The correct pc will be set when the skeleton frame is completely filled out aoqi@0: // The final pc we store in the loop is wrong and will be overwritten below aoqi@0: frame_pcs[number_of_frames - 1 - index ] = Interpreter::deopt_entry(vtos, 0) - frame::pc_return_offset; aoqi@0: aoqi@0: callee_parameters = array->element(index)->method()->size_of_parameters(); aoqi@0: callee_locals = array->element(index)->method()->max_locals(); aoqi@0: popframe_extra_args = 0; aoqi@0: } aoqi@0: aoqi@0: // Compute whether the root vframe returns a float or double value. aoqi@0: BasicType return_type; aoqi@0: { aoqi@0: HandleMark hm; aoqi@0: methodHandle method(thread, array->element(0)->method()); aoqi@0: Bytecode_invoke invoke = Bytecode_invoke_check(method, array->element(0)->bci()); aoqi@0: return_type = invoke.is_valid() ? invoke.result_type() : T_ILLEGAL; aoqi@0: } aoqi@0: aoqi@0: // Compute information for handling adapters and adjusting the frame size of the caller. aoqi@0: int caller_adjustment = 0; aoqi@0: aoqi@0: // Compute the amount the oldest interpreter frame will have to adjust aoqi@0: // its caller's stack by. If the caller is a compiled frame then aoqi@0: // we pretend that the callee has no parameters so that the aoqi@0: // extension counts for the full amount of locals and not just aoqi@0: // locals-parms. This is because without a c2i adapter the parm aoqi@0: // area as created by the compiled frame will not be usable by aoqi@0: // the interpreter. (Depending on the calling convention there aoqi@0: // may not even be enough space). aoqi@0: aoqi@0: // QQQ I'd rather see this pushed down into last_frame_adjust aoqi@0: // and have it take the sender (aka caller). aoqi@0: aoqi@0: if (deopt_sender.is_compiled_frame() || caller_was_method_handle) { aoqi@0: caller_adjustment = last_frame_adjust(0, callee_locals); aoqi@0: } else if (callee_locals > callee_parameters) { aoqi@0: // The caller frame may need extending to accommodate aoqi@0: // non-parameter locals of the first unpacked interpreted frame. aoqi@0: // Compute that adjustment. aoqi@0: caller_adjustment = last_frame_adjust(callee_parameters, callee_locals); aoqi@0: } aoqi@0: aoqi@0: // If the sender is deoptimized the we must retrieve the address of the handler aoqi@0: // since the frame will "magically" show the original pc before the deopt aoqi@0: // and we'd undo the deopt. aoqi@0: aoqi@0: frame_pcs[0] = deopt_sender.raw_pc(); aoqi@0: aoqi@0: #ifndef SHARK aoqi@0: assert(CodeCache::find_blob_unsafe(frame_pcs[0]) != NULL, "bad pc"); aoqi@0: #endif // SHARK aoqi@0: aoqi@0: UnrollBlock* info = new UnrollBlock(array->frame_size() * BytesPerWord, aoqi@0: caller_adjustment * BytesPerWord, aoqi@0: caller_was_method_handle ? 0 : callee_parameters, aoqi@0: number_of_frames, aoqi@0: frame_sizes, aoqi@0: frame_pcs, aoqi@0: return_type); aoqi@0: // On some platforms, we need a way to pass some platform dependent aoqi@0: // information to the unpacking code so the skeletal frames come out aoqi@0: // correct (initial fp value, unextended sp, ...) aoqi@0: info->set_initial_info((intptr_t) array->sender().initial_deoptimization_info()); aoqi@0: aoqi@0: if (array->frames() > 1) { aoqi@0: if (VerifyStack && TraceDeoptimization) { aoqi@0: ttyLocker ttyl; aoqi@0: tty->print_cr("Deoptimizing method containing inlining"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: array->set_unroll_block(info); aoqi@0: return info; aoqi@0: } aoqi@0: aoqi@0: // Called to cleanup deoptimization data structures in normal case aoqi@0: // after unpacking to stack and when stack overflow error occurs aoqi@0: void Deoptimization::cleanup_deopt_info(JavaThread *thread, aoqi@0: vframeArray *array) { aoqi@0: aoqi@0: // Get array if coming from exception aoqi@0: if (array == NULL) { aoqi@0: array = thread->vframe_array_head(); aoqi@0: } aoqi@0: thread->set_vframe_array_head(NULL); aoqi@0: aoqi@0: // Free the previous UnrollBlock aoqi@0: vframeArray* old_array = thread->vframe_array_last(); aoqi@0: thread->set_vframe_array_last(array); aoqi@0: aoqi@0: if (old_array != NULL) { aoqi@0: UnrollBlock* old_info = old_array->unroll_block(); aoqi@0: old_array->set_unroll_block(NULL); aoqi@0: delete old_info; aoqi@0: delete old_array; aoqi@0: } aoqi@0: aoqi@0: // Deallocate any resource creating in this routine and any ResourceObjs allocated aoqi@0: // inside the vframeArray (StackValueCollections) aoqi@0: aoqi@0: delete thread->deopt_mark(); aoqi@0: thread->set_deopt_mark(NULL); aoqi@0: thread->set_deopt_nmethod(NULL); aoqi@0: aoqi@0: aoqi@0: if (JvmtiExport::can_pop_frame()) { aoqi@0: #ifndef CC_INTERP aoqi@0: // Regardless of whether we entered this routine with the pending aoqi@0: // popframe condition bit set, we should always clear it now aoqi@0: thread->clear_popframe_condition(); aoqi@0: #else aoqi@0: // C++ interpeter will clear has_pending_popframe when it enters aoqi@0: // with method_resume. For deopt_resume2 we clear it now. aoqi@0: if (thread->popframe_forcing_deopt_reexecution()) aoqi@0: thread->clear_popframe_condition(); aoqi@0: #endif /* CC_INTERP */ aoqi@0: } aoqi@0: aoqi@0: // unpack_frames() is called at the end of the deoptimization handler aoqi@0: // and (in C2) at the end of the uncommon trap handler. Note this fact aoqi@0: // so that an asynchronous stack walker can work again. This counter is aoqi@0: // incremented at the beginning of fetch_unroll_info() and (in C2) at aoqi@0: // the beginning of uncommon_trap(). aoqi@0: thread->dec_in_deopt_handler(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Return BasicType of value being returned aoqi@0: JRT_LEAF(BasicType, Deoptimization::unpack_frames(JavaThread* thread, int exec_mode)) aoqi@0: aoqi@0: // We are already active int he special DeoptResourceMark any ResourceObj's we aoqi@0: // allocate will be freed at the end of the routine. aoqi@0: aoqi@0: // It is actually ok to allocate handles in a leaf method. It causes no safepoints, aoqi@0: // but makes the entry a little slower. There is however a little dance we have to aoqi@0: // do in debug mode to get around the NoHandleMark code in the JRT_LEAF macro aoqi@0: ResetNoHandleMark rnhm; // No-op in release/product versions aoqi@0: HandleMark hm; aoqi@0: aoqi@0: frame stub_frame = thread->last_frame(); aoqi@0: aoqi@0: // Since the frame to unpack is the top frame of this thread, the vframe_array_head aoqi@0: // must point to the vframeArray for the unpack frame. aoqi@0: vframeArray* array = thread->vframe_array_head(); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: if (TraceDeoptimization) { aoqi@0: ttyLocker ttyl; aoqi@0: tty->print_cr("DEOPT UNPACKING thread " INTPTR_FORMAT " vframeArray " INTPTR_FORMAT " mode %d", thread, array, exec_mode); aoqi@0: } aoqi@0: #endif aoqi@0: Events::log(thread, "DEOPT UNPACKING pc=" INTPTR_FORMAT " sp=" INTPTR_FORMAT " mode %d", aoqi@0: stub_frame.pc(), stub_frame.sp(), exec_mode); aoqi@0: aoqi@0: UnrollBlock* info = array->unroll_block(); aoqi@0: aoqi@0: // Unpack the interpreter frames and any adapter frame (c2 only) we might create. aoqi@0: array->unpack_to_stack(stub_frame, exec_mode, info->caller_actual_parameters()); aoqi@0: aoqi@0: BasicType bt = info->return_type(); aoqi@0: aoqi@0: // If we have an exception pending, claim that the return type is an oop aoqi@0: // so the deopt_blob does not overwrite the exception_oop. aoqi@0: aoqi@0: if (exec_mode == Unpack_exception) aoqi@0: bt = T_OBJECT; aoqi@0: aoqi@0: // Cleanup thread deopt data aoqi@0: cleanup_deopt_info(thread, array); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: if (VerifyStack) { aoqi@0: ResourceMark res_mark; aoqi@0: aoqi@0: thread->validate_frame_layout(); aoqi@0: aoqi@0: // Verify that the just-unpacked frames match the interpreter's aoqi@0: // notions of expression stack and locals aoqi@0: vframeArray* cur_array = thread->vframe_array_last(); aoqi@0: RegisterMap rm(thread, false); aoqi@0: rm.set_include_argument_oops(false); aoqi@0: bool is_top_frame = true; aoqi@0: int callee_size_of_parameters = 0; aoqi@0: int callee_max_locals = 0; aoqi@0: for (int i = 0; i < cur_array->frames(); i++) { aoqi@0: vframeArrayElement* el = cur_array->element(i); aoqi@0: frame* iframe = el->iframe(); aoqi@0: guarantee(iframe->is_interpreted_frame(), "Wrong frame type"); aoqi@0: aoqi@0: // Get the oop map for this bci aoqi@0: InterpreterOopMap mask; aoqi@0: int cur_invoke_parameter_size = 0; aoqi@0: bool try_next_mask = false; aoqi@0: int next_mask_expression_stack_size = -1; aoqi@0: int top_frame_expression_stack_adjustment = 0; aoqi@0: methodHandle mh(thread, iframe->interpreter_frame_method()); aoqi@0: OopMapCache::compute_one_oop_map(mh, iframe->interpreter_frame_bci(), &mask); aoqi@0: BytecodeStream str(mh); aoqi@0: str.set_start(iframe->interpreter_frame_bci()); aoqi@0: int max_bci = mh->code_size(); aoqi@0: // Get to the next bytecode if possible aoqi@0: assert(str.bci() < max_bci, "bci in interpreter frame out of bounds"); aoqi@0: // Check to see if we can grab the number of outgoing arguments aoqi@0: // at an uncommon trap for an invoke (where the compiler aoqi@0: // generates debug info before the invoke has executed) aoqi@0: Bytecodes::Code cur_code = str.next(); aoqi@0: if (cur_code == Bytecodes::_invokevirtual || aoqi@0: cur_code == Bytecodes::_invokespecial || aoqi@0: cur_code == Bytecodes::_invokestatic || aoqi@0: cur_code == Bytecodes::_invokeinterface || aoqi@0: cur_code == Bytecodes::_invokedynamic) { aoqi@0: Bytecode_invoke invoke(mh, iframe->interpreter_frame_bci()); aoqi@0: Symbol* signature = invoke.signature(); aoqi@0: ArgumentSizeComputer asc(signature); aoqi@0: cur_invoke_parameter_size = asc.size(); aoqi@0: if (invoke.has_receiver()) { aoqi@0: // Add in receiver aoqi@0: ++cur_invoke_parameter_size; aoqi@0: } aoqi@0: if (i != 0 && !invoke.is_invokedynamic() && MethodHandles::has_member_arg(invoke.klass(), invoke.name())) { aoqi@0: callee_size_of_parameters++; aoqi@0: } aoqi@0: } aoqi@0: if (str.bci() < max_bci) { aoqi@0: Bytecodes::Code bc = str.next(); aoqi@0: if (bc >= 0) { aoqi@0: // The interpreter oop map generator reports results before aoqi@0: // the current bytecode has executed except in the case of aoqi@0: // calls. It seems to be hard to tell whether the compiler aoqi@0: // has emitted debug information matching the "state before" aoqi@0: // a given bytecode or the state after, so we try both aoqi@0: switch (cur_code) { aoqi@0: case Bytecodes::_invokevirtual: aoqi@0: case Bytecodes::_invokespecial: aoqi@0: case Bytecodes::_invokestatic: aoqi@0: case Bytecodes::_invokeinterface: aoqi@0: case Bytecodes::_invokedynamic: aoqi@0: case Bytecodes::_athrow: aoqi@0: break; aoqi@0: default: { aoqi@0: InterpreterOopMap next_mask; aoqi@0: OopMapCache::compute_one_oop_map(mh, str.bci(), &next_mask); aoqi@0: next_mask_expression_stack_size = next_mask.expression_stack_size(); aoqi@0: // Need to subtract off the size of the result type of aoqi@0: // the bytecode because this is not described in the aoqi@0: // debug info but returned to the interpreter in the TOS aoqi@0: // caching register aoqi@0: BasicType bytecode_result_type = Bytecodes::result_type(cur_code); aoqi@0: if (bytecode_result_type != T_ILLEGAL) { aoqi@0: top_frame_expression_stack_adjustment = type2size[bytecode_result_type]; aoqi@0: } aoqi@0: assert(top_frame_expression_stack_adjustment >= 0, ""); aoqi@0: try_next_mask = true; aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Verify stack depth and oops in frame aoqi@0: // This assertion may be dependent on the platform we're running on and may need modification (tested on x86 and sparc) aoqi@0: if (!( aoqi@0: /* SPARC */ aoqi@0: (iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + callee_size_of_parameters) || aoqi@0: /* x86 */ aoqi@0: (iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + callee_max_locals) || aoqi@0: (try_next_mask && aoqi@0: (iframe->interpreter_frame_expression_stack_size() == (next_mask_expression_stack_size - aoqi@0: top_frame_expression_stack_adjustment))) || aoqi@0: (is_top_frame && (exec_mode == Unpack_exception) && iframe->interpreter_frame_expression_stack_size() == 0) || aoqi@123: (is_top_frame && (exec_mode == Unpack_uncommon_trap || exec_mode == Unpack_reexecute || el->should_reexecute()) && aoqi@0: (iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + cur_invoke_parameter_size)) aoqi@0: )) { aoqi@0: ttyLocker ttyl; aoqi@0: aoqi@0: // Print out some information that will help us debug the problem aoqi@0: tty->print_cr("Wrong number of expression stack elements during deoptimization"); aoqi@0: tty->print_cr(" Error occurred while verifying frame %d (0..%d, 0 is topmost)", i, cur_array->frames() - 1); aoqi@0: tty->print_cr(" Fabricated interpreter frame had %d expression stack elements", aoqi@0: iframe->interpreter_frame_expression_stack_size()); aoqi@0: tty->print_cr(" Interpreter oop map had %d expression stack elements", mask.expression_stack_size()); aoqi@0: tty->print_cr(" try_next_mask = %d", try_next_mask); aoqi@0: tty->print_cr(" next_mask_expression_stack_size = %d", next_mask_expression_stack_size); aoqi@0: tty->print_cr(" callee_size_of_parameters = %d", callee_size_of_parameters); aoqi@0: tty->print_cr(" callee_max_locals = %d", callee_max_locals); aoqi@0: tty->print_cr(" top_frame_expression_stack_adjustment = %d", top_frame_expression_stack_adjustment); aoqi@0: tty->print_cr(" exec_mode = %d", exec_mode); aoqi@0: tty->print_cr(" cur_invoke_parameter_size = %d", cur_invoke_parameter_size); aoqi@0: tty->print_cr(" Thread = " INTPTR_FORMAT ", thread ID = " UINTX_FORMAT, thread, thread->osthread()->thread_id()); aoqi@0: tty->print_cr(" Interpreted frames:"); aoqi@0: for (int k = 0; k < cur_array->frames(); k++) { aoqi@0: vframeArrayElement* el = cur_array->element(k); aoqi@0: tty->print_cr(" %s (bci %d)", el->method()->name_and_sig_as_C_string(), el->bci()); aoqi@0: } aoqi@0: cur_array->print_on_2(tty); aoqi@0: guarantee(false, "wrong number of expression stack elements during deopt"); aoqi@0: } aoqi@0: VerifyOopClosure verify; aoqi@0: iframe->oops_interpreted_do(&verify, NULL, &rm, false); aoqi@0: callee_size_of_parameters = mh->size_of_parameters(); aoqi@0: callee_max_locals = mh->max_locals(); aoqi@0: is_top_frame = false; aoqi@0: } aoqi@0: } aoqi@0: #endif /* !PRODUCT */ aoqi@0: aoqi@0: aoqi@0: return bt; aoqi@0: JRT_END aoqi@0: aoqi@0: aoqi@0: int Deoptimization::deoptimize_dependents() { aoqi@0: Threads::deoptimized_wrt_marked_nmethods(); aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifdef COMPILER2 aoqi@0: bool Deoptimization::realloc_objects(JavaThread* thread, frame* fr, GrowableArray* objects, TRAPS) { aoqi@0: Handle pending_exception(thread->pending_exception()); aoqi@0: const char* exception_file = thread->exception_file(); aoqi@0: int exception_line = thread->exception_line(); aoqi@0: thread->clear_pending_exception(); aoqi@0: roland@7419: bool failures = false; roland@7419: aoqi@0: for (int i = 0; i < objects->length(); i++) { aoqi@0: assert(objects->at(i)->is_object(), "invalid debug information"); aoqi@0: ObjectValue* sv = (ObjectValue*) objects->at(i); aoqi@0: aoqi@0: KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()())); aoqi@0: oop obj = NULL; aoqi@0: aoqi@0: if (k->oop_is_instance()) { aoqi@0: InstanceKlass* ik = InstanceKlass::cast(k()); roland@7419: obj = ik->allocate_instance(THREAD); aoqi@0: } else if (k->oop_is_typeArray()) { aoqi@0: TypeArrayKlass* ak = TypeArrayKlass::cast(k()); aoqi@0: assert(sv->field_size() % type2size[ak->element_type()] == 0, "non-integral array length"); aoqi@0: int len = sv->field_size() / type2size[ak->element_type()]; roland@7419: obj = ak->allocate(len, THREAD); aoqi@0: } else if (k->oop_is_objArray()) { aoqi@0: ObjArrayKlass* ak = ObjArrayKlass::cast(k()); roland@7419: obj = ak->allocate(sv->field_size(), THREAD); aoqi@0: } aoqi@0: roland@7419: if (obj == NULL) { roland@7419: failures = true; roland@7419: } roland@7419: aoqi@0: assert(sv->value().is_null(), "redundant reallocation"); roland@7419: assert(obj != NULL || HAS_PENDING_EXCEPTION, "allocation should succeed or we should get an exception"); roland@7419: CLEAR_PENDING_EXCEPTION; aoqi@0: sv->set_value(obj); aoqi@0: } aoqi@0: roland@7419: if (failures) { roland@7419: THROW_OOP_(Universe::out_of_memory_error_realloc_objects(), failures); roland@7419: } else if (pending_exception.not_null()) { aoqi@0: thread->set_pending_exception(pending_exception(), exception_file, exception_line); aoqi@0: } aoqi@0: roland@7419: return failures; aoqi@0: } aoqi@0: aoqi@0: // This assumes that the fields are stored in ObjectValue in the same order aoqi@0: // they are yielded by do_nonstatic_fields. aoqi@0: class FieldReassigner: public FieldClosure { aoqi@0: frame* _fr; aoqi@0: RegisterMap* _reg_map; aoqi@0: ObjectValue* _sv; aoqi@0: InstanceKlass* _ik; aoqi@0: oop _obj; aoqi@0: aoqi@0: int _i; aoqi@0: public: aoqi@0: FieldReassigner(frame* fr, RegisterMap* reg_map, ObjectValue* sv, oop obj) : aoqi@0: _fr(fr), _reg_map(reg_map), _sv(sv), _obj(obj), _i(0) {} aoqi@0: aoqi@0: int i() const { return _i; } aoqi@0: aoqi@0: aoqi@0: void do_field(fieldDescriptor* fd) { aoqi@0: intptr_t val; aoqi@0: StackValue* value = aoqi@0: StackValue::create_stack_value(_fr, _reg_map, _sv->field_at(i())); aoqi@0: int offset = fd->offset(); aoqi@0: switch (fd->field_type()) { aoqi@0: case T_OBJECT: case T_ARRAY: aoqi@0: assert(value->type() == T_OBJECT, "Agreement."); aoqi@0: _obj->obj_field_put(offset, value->get_obj()()); aoqi@0: break; aoqi@0: aoqi@0: case T_LONG: case T_DOUBLE: { aoqi@0: assert(value->type() == T_INT, "Agreement."); aoqi@0: StackValue* low = aoqi@0: StackValue::create_stack_value(_fr, _reg_map, _sv->field_at(++_i)); aoqi@0: #ifdef _LP64 aoqi@0: jlong res = (jlong)low->get_int(); aoqi@0: #else aoqi@0: #ifdef SPARC aoqi@0: // For SPARC we have to swap high and low words. aoqi@0: jlong res = jlong_from((jint)low->get_int(), (jint)value->get_int()); aoqi@0: #else aoqi@0: jlong res = jlong_from((jint)value->get_int(), (jint)low->get_int()); aoqi@0: #endif //SPARC aoqi@0: #endif aoqi@0: _obj->long_field_put(offset, res); aoqi@0: break; aoqi@0: } aoqi@0: // Have to cast to INT (32 bits) pointer to avoid little/big-endian problem. aoqi@0: case T_INT: case T_FLOAT: // 4 bytes. aoqi@0: assert(value->type() == T_INT, "Agreement."); aoqi@0: val = value->get_int(); aoqi@0: _obj->int_field_put(offset, (jint)*((jint*)&val)); aoqi@0: break; aoqi@0: kevinw@8395: case T_SHORT: aoqi@0: assert(value->type() == T_INT, "Agreement."); aoqi@0: val = value->get_int(); aoqi@0: _obj->short_field_put(offset, (jshort)*((jint*)&val)); aoqi@0: break; aoqi@0: kevinw@8395: case T_CHAR: kevinw@8395: assert(value->type() == T_INT, "Agreement."); kevinw@8395: val = value->get_int(); kevinw@8395: _obj->char_field_put(offset, (jchar)*((jint*)&val)); kevinw@8395: break; kevinw@8395: kevinw@8395: case T_BYTE: kevinw@8395: assert(value->type() == T_INT, "Agreement."); kevinw@8395: val = value->get_int(); kevinw@8395: _obj->byte_field_put(offset, (jbyte)*((jint*)&val)); kevinw@8395: break; kevinw@8395: kevinw@8395: case T_BOOLEAN: aoqi@0: assert(value->type() == T_INT, "Agreement."); aoqi@0: val = value->get_int(); aoqi@0: _obj->bool_field_put(offset, (jboolean)*((jint*)&val)); aoqi@0: break; aoqi@0: aoqi@0: default: aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: _i++; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: // restore elements of an eliminated type array aoqi@0: void Deoptimization::reassign_type_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, typeArrayOop obj, BasicType type) { aoqi@0: int index = 0; aoqi@0: intptr_t val; aoqi@0: aoqi@0: for (int i = 0; i < sv->field_size(); i++) { aoqi@0: StackValue* value = StackValue::create_stack_value(fr, reg_map, sv->field_at(i)); aoqi@0: switch(type) { aoqi@0: case T_LONG: case T_DOUBLE: { aoqi@0: assert(value->type() == T_INT, "Agreement."); aoqi@0: StackValue* low = aoqi@0: StackValue::create_stack_value(fr, reg_map, sv->field_at(++i)); aoqi@0: #ifdef _LP64 aoqi@0: jlong res = (jlong)low->get_int(); aoqi@0: #else aoqi@0: #ifdef SPARC aoqi@0: // For SPARC we have to swap high and low words. aoqi@0: jlong res = jlong_from((jint)low->get_int(), (jint)value->get_int()); aoqi@0: #else aoqi@0: jlong res = jlong_from((jint)value->get_int(), (jint)low->get_int()); aoqi@0: #endif //SPARC aoqi@0: #endif aoqi@0: obj->long_at_put(index, res); aoqi@0: break; aoqi@0: } aoqi@0: aoqi@0: // Have to cast to INT (32 bits) pointer to avoid little/big-endian problem. aoqi@0: case T_INT: case T_FLOAT: // 4 bytes. aoqi@0: assert(value->type() == T_INT, "Agreement."); aoqi@0: val = value->get_int(); aoqi@0: obj->int_at_put(index, (jint)*((jint*)&val)); aoqi@0: break; aoqi@0: kevinw@8395: case T_SHORT: aoqi@0: assert(value->type() == T_INT, "Agreement."); aoqi@0: val = value->get_int(); aoqi@0: obj->short_at_put(index, (jshort)*((jint*)&val)); aoqi@0: break; aoqi@0: kevinw@8395: case T_CHAR: kevinw@8395: assert(value->type() == T_INT, "Agreement."); kevinw@8395: val = value->get_int(); kevinw@8395: obj->char_at_put(index, (jchar)*((jint*)&val)); kevinw@8395: break; kevinw@8395: kevinw@8395: case T_BYTE: kevinw@8395: assert(value->type() == T_INT, "Agreement."); kevinw@8395: val = value->get_int(); kevinw@8395: obj->byte_at_put(index, (jbyte)*((jint*)&val)); kevinw@8395: break; kevinw@8395: kevinw@8395: case T_BOOLEAN: aoqi@0: assert(value->type() == T_INT, "Agreement."); aoqi@0: val = value->get_int(); aoqi@0: obj->bool_at_put(index, (jboolean)*((jint*)&val)); aoqi@0: break; aoqi@0: aoqi@0: default: aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: index++; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // restore fields of an eliminated object array aoqi@0: void Deoptimization::reassign_object_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, objArrayOop obj) { aoqi@0: for (int i = 0; i < sv->field_size(); i++) { aoqi@0: StackValue* value = StackValue::create_stack_value(fr, reg_map, sv->field_at(i)); aoqi@0: assert(value->type() == T_OBJECT, "object element expected"); aoqi@0: obj->obj_at_put(i, value->get_obj()()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // restore fields of all eliminated objects and arrays roland@7419: void Deoptimization::reassign_fields(frame* fr, RegisterMap* reg_map, GrowableArray* objects, bool realloc_failures) { aoqi@0: for (int i = 0; i < objects->length(); i++) { aoqi@0: ObjectValue* sv = (ObjectValue*) objects->at(i); aoqi@0: KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()())); aoqi@0: Handle obj = sv->value(); roland@7419: assert(obj.not_null() || realloc_failures, "reallocation was missed"); roland@7419: if (obj.is_null()) { roland@7419: continue; roland@7419: } aoqi@0: aoqi@0: if (k->oop_is_instance()) { aoqi@0: InstanceKlass* ik = InstanceKlass::cast(k()); aoqi@0: FieldReassigner reassign(fr, reg_map, sv, obj()); aoqi@0: ik->do_nonstatic_fields(&reassign); aoqi@0: } else if (k->oop_is_typeArray()) { aoqi@0: TypeArrayKlass* ak = TypeArrayKlass::cast(k()); aoqi@0: reassign_type_array_elements(fr, reg_map, sv, (typeArrayOop) obj(), ak->element_type()); aoqi@0: } else if (k->oop_is_objArray()) { aoqi@0: reassign_object_array_elements(fr, reg_map, sv, (objArrayOop) obj()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // relock objects for which synchronization was eliminated roland@7419: void Deoptimization::relock_objects(GrowableArray* monitors, JavaThread* thread, bool realloc_failures) { aoqi@0: for (int i = 0; i < monitors->length(); i++) { aoqi@0: MonitorInfo* mon_info = monitors->at(i); aoqi@0: if (mon_info->eliminated()) { roland@7419: assert(!mon_info->owner_is_scalar_replaced() || realloc_failures, "reallocation was missed"); roland@7419: if (!mon_info->owner_is_scalar_replaced()) { roland@7419: Handle obj = Handle(mon_info->owner()); roland@7419: markOop mark = obj->mark(); roland@7419: if (UseBiasedLocking && mark->has_bias_pattern()) { roland@7419: // New allocated objects may have the mark set to anonymously biased. roland@7419: // Also the deoptimized method may called methods with synchronization roland@7419: // where the thread-local object is bias locked to the current thread. roland@7419: assert(mark->is_biased_anonymously() || roland@7419: mark->biased_locker() == thread, "should be locked to current thread"); roland@7419: // Reset mark word to unbiased prototype. roland@7419: markOop unbiased_prototype = markOopDesc::prototype()->set_age(mark->age()); roland@7419: obj->set_mark(unbiased_prototype); roland@7419: } roland@7419: BasicLock* lock = mon_info->lock(); roland@7419: ObjectSynchronizer::slow_enter(obj, lock, thread); roland@7419: assert(mon_info->owner()->is_locked(), "object must be locked now"); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: // print information about reallocated objects roland@7419: void Deoptimization::print_objects(GrowableArray* objects, bool realloc_failures) { aoqi@0: fieldDescriptor fd; aoqi@0: aoqi@0: for (int i = 0; i < objects->length(); i++) { aoqi@0: ObjectValue* sv = (ObjectValue*) objects->at(i); aoqi@0: KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()())); aoqi@0: Handle obj = sv->value(); aoqi@0: aoqi@0: tty->print(" object <" INTPTR_FORMAT "> of type ", (void *)sv->value()()); aoqi@0: k->print_value(); roland@7419: assert(obj.not_null() || realloc_failures, "reallocation was missed"); roland@7419: if (obj.is_null()) { roland@7419: tty->print(" allocation failed"); roland@7419: } else { roland@7419: tty->print(" allocated (%d bytes)", obj->size() * HeapWordSize); roland@7419: } aoqi@0: tty->cr(); aoqi@0: roland@7419: if (Verbose && !obj.is_null()) { aoqi@0: k->oop_print_on(obj(), tty); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: #endif aoqi@0: #endif // COMPILER2 aoqi@0: roland@7419: vframeArray* Deoptimization::create_vframeArray(JavaThread* thread, frame fr, RegisterMap *reg_map, GrowableArray* chunk, bool realloc_failures) { aoqi@0: Events::log(thread, "DEOPT PACKING pc=" INTPTR_FORMAT " sp=" INTPTR_FORMAT, fr.pc(), fr.sp()); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: if (TraceDeoptimization) { aoqi@0: ttyLocker ttyl; aoqi@0: tty->print("DEOPT PACKING thread " INTPTR_FORMAT " ", thread); aoqi@0: fr.print_on(tty); aoqi@0: tty->print_cr(" Virtual frames (innermost first):"); aoqi@0: for (int index = 0; index < chunk->length(); index++) { aoqi@0: compiledVFrame* vf = chunk->at(index); aoqi@0: tty->print(" %2d - ", index); aoqi@0: vf->print_value(); aoqi@0: int bci = chunk->at(index)->raw_bci(); aoqi@0: const char* code_name; aoqi@0: if (bci == SynchronizationEntryBCI) { aoqi@0: code_name = "sync entry"; aoqi@0: } else { aoqi@0: Bytecodes::Code code = vf->method()->code_at(bci); aoqi@0: code_name = Bytecodes::name(code); aoqi@0: } aoqi@0: tty->print(" - %s", code_name); aoqi@0: tty->print_cr(" @ bci %d ", bci); aoqi@0: if (Verbose) { aoqi@0: vf->print(); aoqi@0: tty->cr(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: // Register map for next frame (used for stack crawl). We capture aoqi@0: // the state of the deopt'ing frame's caller. Thus if we need to aoqi@0: // stuff a C2I adapter we can properly fill in the callee-save aoqi@0: // register locations. aoqi@0: frame caller = fr.sender(reg_map); aoqi@0: int frame_size = caller.sp() - fr.sp(); aoqi@0: aoqi@0: frame sender = caller; aoqi@0: aoqi@0: // Since the Java thread being deoptimized will eventually adjust it's own stack, aoqi@0: // the vframeArray containing the unpacking information is allocated in the C heap. aoqi@0: // For Compiler1, the caller of the deoptimized frame is saved for use by unpack_frames(). roland@7419: vframeArray* array = vframeArray::allocate(thread, frame_size, chunk, reg_map, sender, caller, fr, realloc_failures); aoqi@0: aoqi@0: // Compare the vframeArray to the collected vframes aoqi@0: assert(array->structural_compare(thread, chunk), "just checking"); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: if (TraceDeoptimization) { aoqi@0: ttyLocker ttyl; aoqi@0: tty->print_cr(" Created vframeArray " INTPTR_FORMAT, array); aoqi@0: } aoqi@0: #endif // PRODUCT aoqi@0: aoqi@0: return array; aoqi@0: } aoqi@0: roland@7419: #ifdef COMPILER2 roland@7419: void Deoptimization::pop_frames_failed_reallocs(JavaThread* thread, vframeArray* array) { roland@7419: // Reallocation of some scalar replaced objects failed. Record roland@7419: // that we need to pop all the interpreter frames for the roland@7419: // deoptimized compiled frame. roland@7419: assert(thread->frames_to_pop_failed_realloc() == 0, "missed frames to pop?"); roland@7419: thread->set_frames_to_pop_failed_realloc(array->frames()); roland@7419: // Unlock all monitors here otherwise the interpreter will see a roland@7419: // mix of locked and unlocked monitors (because of failed roland@7419: // reallocations of synchronized objects) and be confused. roland@7419: for (int i = 0; i < array->frames(); i++) { roland@7419: MonitorChunk* monitors = array->element(i)->monitors(); roland@7419: if (monitors != NULL) { roland@7419: for (int j = 0; j < monitors->number_of_monitors(); j++) { roland@7419: BasicObjectLock* src = monitors->at(j); roland@7419: if (src->obj() != NULL) { roland@7419: ObjectSynchronizer::fast_exit(src->obj(), src->lock(), thread); roland@7419: } roland@7419: } roland@7419: array->element(i)->free_monitors(thread); roland@7419: #ifdef ASSERT roland@7419: array->element(i)->set_removed_monitors(); roland@7419: #endif roland@7419: } roland@7419: } roland@7419: } roland@7419: #endif aoqi@0: aoqi@0: static void collect_monitors(compiledVFrame* cvf, GrowableArray* objects_to_revoke) { aoqi@0: GrowableArray* monitors = cvf->monitors(); aoqi@0: for (int i = 0; i < monitors->length(); i++) { aoqi@0: MonitorInfo* mon_info = monitors->at(i); aoqi@0: if (!mon_info->eliminated() && mon_info->owner() != NULL) { aoqi@0: objects_to_revoke->append(Handle(mon_info->owner())); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void Deoptimization::revoke_biases_of_monitors(JavaThread* thread, frame fr, RegisterMap* map) { aoqi@0: if (!UseBiasedLocking) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: GrowableArray* objects_to_revoke = new GrowableArray(); aoqi@0: aoqi@0: // Unfortunately we don't have a RegisterMap available in most of aoqi@0: // the places we want to call this routine so we need to walk the aoqi@0: // stack again to update the register map. aoqi@0: if (map == NULL || !map->update_map()) { aoqi@0: StackFrameStream sfs(thread, true); aoqi@0: bool found = false; aoqi@0: while (!found && !sfs.is_done()) { aoqi@0: frame* cur = sfs.current(); aoqi@0: sfs.next(); aoqi@0: found = cur->id() == fr.id(); aoqi@0: } aoqi@0: assert(found, "frame to be deoptimized not found on target thread's stack"); aoqi@0: map = sfs.register_map(); aoqi@0: } aoqi@0: aoqi@0: vframe* vf = vframe::new_vframe(&fr, map, thread); aoqi@0: compiledVFrame* cvf = compiledVFrame::cast(vf); aoqi@0: // Revoke monitors' biases in all scopes aoqi@0: while (!cvf->is_top()) { aoqi@0: collect_monitors(cvf, objects_to_revoke); aoqi@0: cvf = compiledVFrame::cast(cvf->sender()); aoqi@0: } aoqi@0: collect_monitors(cvf, objects_to_revoke); aoqi@0: aoqi@0: if (SafepointSynchronize::is_at_safepoint()) { aoqi@0: BiasedLocking::revoke_at_safepoint(objects_to_revoke); aoqi@0: } else { aoqi@0: BiasedLocking::revoke(objects_to_revoke); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void Deoptimization::revoke_biases_of_monitors(CodeBlob* cb) { aoqi@0: if (!UseBiasedLocking) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: assert(SafepointSynchronize::is_at_safepoint(), "must only be called from safepoint"); aoqi@0: GrowableArray* objects_to_revoke = new GrowableArray(); aoqi@0: for (JavaThread* jt = Threads::first(); jt != NULL ; jt = jt->next()) { aoqi@0: if (jt->has_last_Java_frame()) { aoqi@0: StackFrameStream sfs(jt, true); aoqi@0: while (!sfs.is_done()) { aoqi@0: frame* cur = sfs.current(); aoqi@0: if (cb->contains(cur->pc())) { aoqi@0: vframe* vf = vframe::new_vframe(cur, sfs.register_map(), jt); aoqi@0: compiledVFrame* cvf = compiledVFrame::cast(vf); aoqi@0: // Revoke monitors' biases in all scopes aoqi@0: while (!cvf->is_top()) { aoqi@0: collect_monitors(cvf, objects_to_revoke); aoqi@0: cvf = compiledVFrame::cast(cvf->sender()); aoqi@0: } aoqi@0: collect_monitors(cvf, objects_to_revoke); aoqi@0: } aoqi@0: sfs.next(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: BiasedLocking::revoke_at_safepoint(objects_to_revoke); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr) { aoqi@0: assert(fr.can_be_deoptimized(), "checking frame type"); aoqi@0: aoqi@0: gather_statistics(Reason_constraint, Action_none, Bytecodes::_illegal); aoqi@0: aoqi@0: // Patch the nmethod so that when execution returns to it we will aoqi@0: // deopt the execution state and return to the interpreter. aoqi@0: fr.deoptimize(thread); aoqi@0: } aoqi@0: aoqi@0: void Deoptimization::deoptimize(JavaThread* thread, frame fr, RegisterMap *map) { aoqi@0: // Deoptimize only if the frame comes from compile code. aoqi@0: // Do not deoptimize the frame which is already patched aoqi@0: // during the execution of the loops below. aoqi@0: if (!fr.is_compiled_frame() || fr.is_deoptimized_frame()) { aoqi@0: return; aoqi@0: } aoqi@0: ResourceMark rm; aoqi@0: DeoptimizationMarker dm; aoqi@0: if (UseBiasedLocking) { aoqi@0: revoke_biases_of_monitors(thread, fr, map); aoqi@0: } aoqi@0: deoptimize_single_frame(thread, fr); aoqi@0: aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void Deoptimization::deoptimize_frame_internal(JavaThread* thread, intptr_t* id) { aoqi@0: assert(thread == Thread::current() || SafepointSynchronize::is_at_safepoint(), aoqi@0: "can only deoptimize other thread at a safepoint"); aoqi@0: // Compute frame and register map based on thread and sp. aoqi@0: RegisterMap reg_map(thread, UseBiasedLocking); aoqi@0: frame fr = thread->last_frame(); aoqi@0: while (fr.id() != id) { aoqi@0: fr = fr.sender(®_map); aoqi@0: } aoqi@0: deoptimize(thread, fr, ®_map); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void Deoptimization::deoptimize_frame(JavaThread* thread, intptr_t* id) { aoqi@0: if (thread == Thread::current()) { aoqi@0: Deoptimization::deoptimize_frame_internal(thread, id); aoqi@0: } else { aoqi@0: VM_DeoptimizeFrame deopt(thread, id); aoqi@0: VMThread::execute(&deopt); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // JVMTI PopFrame support aoqi@0: JRT_LEAF(void, Deoptimization::popframe_preserve_args(JavaThread* thread, int bytes_to_save, void* start_address)) aoqi@0: { aoqi@0: thread->popframe_preserve_args(in_ByteSize(bytes_to_save), start_address); aoqi@0: } aoqi@0: JRT_END aoqi@0: aoqi@0: aoqi@0: #if defined(COMPILER2) || defined(SHARK) aoqi@0: void Deoptimization::load_class_by_index(constantPoolHandle constant_pool, int index, TRAPS) { aoqi@0: // in case of an unresolved klass entry, load the class. aoqi@0: if (constant_pool->tag_at(index).is_unresolved_klass()) { aoqi@0: Klass* tk = constant_pool->klass_at(index, CHECK); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: if (!constant_pool->tag_at(index).is_symbol()) return; aoqi@0: aoqi@0: Handle class_loader (THREAD, constant_pool->pool_holder()->class_loader()); aoqi@0: Symbol* symbol = constant_pool->symbol_at(index); aoqi@0: aoqi@0: // class name? aoqi@0: if (symbol->byte_at(0) != '(') { aoqi@0: Handle protection_domain (THREAD, constant_pool->pool_holder()->protection_domain()); aoqi@0: SystemDictionary::resolve_or_null(symbol, class_loader, protection_domain, CHECK); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // then it must be a signature! aoqi@0: ResourceMark rm(THREAD); aoqi@0: for (SignatureStream ss(symbol); !ss.is_done(); ss.next()) { aoqi@0: if (ss.is_object()) { aoqi@0: Symbol* class_name = ss.as_symbol(CHECK); aoqi@0: Handle protection_domain (THREAD, constant_pool->pool_holder()->protection_domain()); aoqi@0: SystemDictionary::resolve_or_null(class_name, class_loader, protection_domain, CHECK); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void Deoptimization::load_class_by_index(constantPoolHandle constant_pool, int index) { aoqi@0: EXCEPTION_MARK; aoqi@0: load_class_by_index(constant_pool, index, THREAD); aoqi@0: if (HAS_PENDING_EXCEPTION) { aoqi@0: // Exception happened during classloading. We ignore the exception here, since it aoqi@0: // is going to be rethrown since the current activation is going to be deoptimized and aoqi@0: // the interpreter will re-execute the bytecode. aoqi@0: CLEAR_PENDING_EXCEPTION; aoqi@0: // Class loading called java code which may have caused a stack aoqi@0: // overflow. If the exception was thrown right before the return aoqi@0: // to the runtime the stack is no longer guarded. Reguard the aoqi@0: // stack otherwise if we return to the uncommon trap blob and the aoqi@0: // stack bang causes a stack overflow we crash. aoqi@0: assert(THREAD->is_Java_thread(), "only a java thread can be here"); aoqi@0: JavaThread* thread = (JavaThread*)THREAD; aoqi@0: bool guard_pages_enabled = thread->stack_yellow_zone_enabled(); aoqi@0: if (!guard_pages_enabled) guard_pages_enabled = thread->reguard_stack(); aoqi@0: assert(guard_pages_enabled, "stack banging in uncommon trap blob may cause crash"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: JRT_ENTRY(void, Deoptimization::uncommon_trap_inner(JavaThread* thread, jint trap_request)) { aoqi@0: HandleMark hm; aoqi@0: aoqi@0: // uncommon_trap() is called at the beginning of the uncommon trap aoqi@0: // handler. Note this fact before we start generating temporary frames aoqi@0: // that can confuse an asynchronous stack walker. This counter is aoqi@0: // decremented at the end of unpack_frames(). aoqi@0: thread->inc_in_deopt_handler(); aoqi@0: aoqi@0: // We need to update the map if we have biased locking. aoqi@0: RegisterMap reg_map(thread, UseBiasedLocking); aoqi@0: frame stub_frame = thread->last_frame(); aoqi@0: frame fr = stub_frame.sender(®_map); aoqi@0: // Make sure the calling nmethod is not getting deoptimized and removed aoqi@0: // before we are done with it. aoqi@0: nmethodLocker nl(fr.pc()); aoqi@0: aoqi@0: // Log a message aoqi@0: Events::log(thread, "Uncommon trap: trap_request=" PTR32_FORMAT " fr.pc=" INTPTR_FORMAT, aoqi@0: trap_request, fr.pc()); aoqi@0: aoqi@0: { aoqi@0: ResourceMark rm; aoqi@0: aoqi@0: // Revoke biases of any monitors in the frame to ensure we can migrate them aoqi@0: revoke_biases_of_monitors(thread, fr, ®_map); aoqi@0: aoqi@0: DeoptReason reason = trap_request_reason(trap_request); aoqi@0: DeoptAction action = trap_request_action(trap_request); aoqi@0: jint unloaded_class_index = trap_request_index(trap_request); // CP idx or -1 aoqi@0: aoqi@0: vframe* vf = vframe::new_vframe(&fr, ®_map, thread); aoqi@0: compiledVFrame* cvf = compiledVFrame::cast(vf); aoqi@0: aoqi@0: nmethod* nm = cvf->code(); aoqi@0: aoqi@0: ScopeDesc* trap_scope = cvf->scope(); aoqi@0: methodHandle trap_method = trap_scope->method(); aoqi@0: int trap_bci = trap_scope->bci(); aoqi@0: Bytecodes::Code trap_bc = trap_method->java_code_at(trap_bci); aoqi@0: aoqi@0: // Record this event in the histogram. aoqi@0: gather_statistics(reason, action, trap_bc); aoqi@0: aoqi@0: // Ensure that we can record deopt. history: aoqi@0: // Need MDO to record RTM code generation state. aoqi@0: bool create_if_missing = ProfileTraps RTM_OPT_ONLY( || UseRTMLocking ); aoqi@0: aoqi@0: MethodData* trap_mdo = aoqi@0: get_method_data(thread, trap_method, create_if_missing); aoqi@0: aoqi@0: // Log a message aoqi@0: Events::log_deopt_message(thread, "Uncommon trap: reason=%s action=%s pc=" INTPTR_FORMAT " method=%s @ %d", aoqi@0: trap_reason_name(reason), trap_action_name(action), fr.pc(), aoqi@0: trap_method->name_and_sig_as_C_string(), trap_bci); aoqi@0: aoqi@0: // Print a bunch of diagnostics, if requested. aoqi@0: if (TraceDeoptimization || LogCompilation) { aoqi@0: ResourceMark rm; aoqi@0: ttyLocker ttyl; aoqi@0: char buf[100]; aoqi@0: if (xtty != NULL) { kevinw@9327: xtty->begin_head("uncommon_trap thread='" UINTX_FORMAT "' %s", aoqi@0: os::current_thread_id(), aoqi@0: format_trap_request(buf, sizeof(buf), trap_request)); aoqi@0: nm->log_identity(xtty); aoqi@0: } aoqi@0: Symbol* class_name = NULL; aoqi@0: bool unresolved = false; aoqi@0: if (unloaded_class_index >= 0) { aoqi@0: constantPoolHandle constants (THREAD, trap_method->constants()); aoqi@0: if (constants->tag_at(unloaded_class_index).is_unresolved_klass()) { aoqi@0: class_name = constants->klass_name_at(unloaded_class_index); aoqi@0: unresolved = true; aoqi@0: if (xtty != NULL) aoqi@0: xtty->print(" unresolved='1'"); aoqi@0: } else if (constants->tag_at(unloaded_class_index).is_symbol()) { aoqi@0: class_name = constants->symbol_at(unloaded_class_index); aoqi@0: } aoqi@0: if (xtty != NULL) aoqi@0: xtty->name(class_name); aoqi@0: } aoqi@0: if (xtty != NULL && trap_mdo != NULL) { aoqi@0: // Dump the relevant MDO state. aoqi@0: // This is the deopt count for the current reason, any previous aoqi@0: // reasons or recompiles seen at this point. aoqi@0: int dcnt = trap_mdo->trap_count(reason); aoqi@0: if (dcnt != 0) aoqi@0: xtty->print(" count='%d'", dcnt); aoqi@0: ProfileData* pdata = trap_mdo->bci_to_data(trap_bci); aoqi@0: int dos = (pdata == NULL)? 0: pdata->trap_state(); aoqi@0: if (dos != 0) { aoqi@0: xtty->print(" state='%s'", format_trap_state(buf, sizeof(buf), dos)); aoqi@0: if (trap_state_is_recompiled(dos)) { aoqi@0: int recnt2 = trap_mdo->overflow_recompile_count(); aoqi@0: if (recnt2 != 0) aoqi@0: xtty->print(" recompiles2='%d'", recnt2); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: if (xtty != NULL) { aoqi@0: xtty->stamp(); aoqi@0: xtty->end_head(); aoqi@0: } aoqi@0: if (TraceDeoptimization) { // make noise on the tty aoqi@0: tty->print("Uncommon trap occurred in"); aoqi@0: nm->method()->print_short_name(tty); aoqi@0: tty->print(" (@" INTPTR_FORMAT ") thread=" UINTX_FORMAT " reason=%s action=%s unloaded_class_index=%d", aoqi@0: fr.pc(), aoqi@0: os::current_thread_id(), aoqi@0: trap_reason_name(reason), aoqi@0: trap_action_name(action), aoqi@0: unloaded_class_index); aoqi@0: if (class_name != NULL) { aoqi@0: tty->print(unresolved ? " unresolved class: " : " symbol: "); aoqi@0: class_name->print_symbol_on(tty); aoqi@0: } aoqi@0: tty->cr(); aoqi@0: } aoqi@0: if (xtty != NULL) { aoqi@0: // Log the precise location of the trap. aoqi@0: for (ScopeDesc* sd = trap_scope; ; sd = sd->sender()) { aoqi@0: xtty->begin_elem("jvms bci='%d'", sd->bci()); aoqi@0: xtty->method(sd->method()); aoqi@0: xtty->end_elem(); aoqi@0: if (sd->is_top()) break; aoqi@0: } aoqi@0: xtty->tail("uncommon_trap"); aoqi@0: } aoqi@0: } aoqi@0: // (End diagnostic printout.) aoqi@0: aoqi@0: // Load class if necessary aoqi@0: if (unloaded_class_index >= 0) { aoqi@0: constantPoolHandle constants(THREAD, trap_method->constants()); aoqi@0: load_class_by_index(constants, unloaded_class_index); aoqi@0: } aoqi@0: aoqi@0: // Flush the nmethod if necessary and desirable. aoqi@0: // aoqi@0: // We need to avoid situations where we are re-flushing the nmethod aoqi@0: // because of a hot deoptimization site. Repeated flushes at the same aoqi@0: // point need to be detected by the compiler and avoided. If the compiler aoqi@0: // cannot avoid them (or has a bug and "refuses" to avoid them), this aoqi@0: // module must take measures to avoid an infinite cycle of recompilation aoqi@0: // and deoptimization. There are several such measures: aoqi@0: // aoqi@0: // 1. If a recompilation is ordered a second time at some site X aoqi@0: // and for the same reason R, the action is adjusted to 'reinterpret', aoqi@0: // to give the interpreter time to exercise the method more thoroughly. aoqi@0: // If this happens, the method's overflow_recompile_count is incremented. aoqi@0: // aoqi@0: // 2. If the compiler fails to reduce the deoptimization rate, then aoqi@0: // the method's overflow_recompile_count will begin to exceed the set aoqi@0: // limit PerBytecodeRecompilationCutoff. If this happens, the action aoqi@0: // is adjusted to 'make_not_compilable', and the method is abandoned aoqi@0: // to the interpreter. This is a performance hit for hot methods, aoqi@0: // but is better than a disastrous infinite cycle of recompilations. aoqi@0: // (Actually, only the method containing the site X is abandoned.) aoqi@0: // aoqi@0: // 3. In parallel with the previous measures, if the total number of aoqi@0: // recompilations of a method exceeds the much larger set limit aoqi@0: // PerMethodRecompilationCutoff, the method is abandoned. aoqi@0: // This should only happen if the method is very large and has aoqi@0: // many "lukewarm" deoptimizations. The code which enforces this aoqi@0: // limit is elsewhere (class nmethod, class Method). aoqi@0: // aoqi@0: // Note that the per-BCI 'is_recompiled' bit gives the compiler one chance aoqi@0: // to recompile at each bytecode independently of the per-BCI cutoff. aoqi@0: // aoqi@0: // The decision to update code is up to the compiler, and is encoded aoqi@0: // in the Action_xxx code. If the compiler requests Action_none aoqi@0: // no trap state is changed, no compiled code is changed, and the aoqi@0: // computation suffers along in the interpreter. aoqi@0: // aoqi@0: // The other action codes specify various tactics for decompilation aoqi@0: // and recompilation. Action_maybe_recompile is the loosest, and aoqi@0: // allows the compiled code to stay around until enough traps are seen, aoqi@0: // and until the compiler gets around to recompiling the trapping method. aoqi@0: // aoqi@0: // The other actions cause immediate removal of the present code. aoqi@0: vlivanov@7890: // Traps caused by injected profile shouldn't pollute trap counts. vlivanov@7890: bool injected_profile_trap = trap_method->has_injected_profile() && vlivanov@7890: (reason == Reason_intrinsic || reason == Reason_unreached); vlivanov@7890: bool update_trap_state = !injected_profile_trap; aoqi@0: bool make_not_entrant = false; aoqi@0: bool make_not_compilable = false; aoqi@0: bool reprofile = false; aoqi@0: switch (action) { aoqi@0: case Action_none: aoqi@0: // Keep the old code. aoqi@0: update_trap_state = false; aoqi@0: break; aoqi@0: case Action_maybe_recompile: aoqi@0: // Do not need to invalidate the present code, but we can aoqi@0: // initiate another aoqi@0: // Start compiler without (necessarily) invalidating the nmethod. aoqi@0: // The system will tolerate the old code, but new code should be aoqi@0: // generated when possible. aoqi@0: break; aoqi@0: case Action_reinterpret: aoqi@0: // Go back into the interpreter for a while, and then consider aoqi@0: // recompiling form scratch. aoqi@0: make_not_entrant = true; aoqi@0: // Reset invocation counter for outer most method. aoqi@0: // This will allow the interpreter to exercise the bytecodes aoqi@0: // for a while before recompiling. aoqi@0: // By contrast, Action_make_not_entrant is immediate. aoqi@0: // aoqi@0: // Note that the compiler will track null_check, null_assert, aoqi@0: // range_check, and class_check events and log them as if they aoqi@0: // had been traps taken from compiled code. This will update aoqi@0: // the MDO trap history so that the next compilation will aoqi@0: // properly detect hot trap sites. aoqi@0: reprofile = true; aoqi@0: break; aoqi@0: case Action_make_not_entrant: aoqi@0: // Request immediate recompilation, and get rid of the old code. aoqi@0: // Make them not entrant, so next time they are called they get aoqi@0: // recompiled. Unloaded classes are loaded now so recompile before next aoqi@0: // time they are called. Same for uninitialized. The interpreter will aoqi@0: // link the missing class, if any. aoqi@0: make_not_entrant = true; aoqi@0: break; aoqi@0: case Action_make_not_compilable: aoqi@0: // Give up on compiling this method at all. aoqi@0: make_not_entrant = true; aoqi@0: make_not_compilable = true; aoqi@0: break; aoqi@0: default: aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: aoqi@0: // Setting +ProfileTraps fixes the following, on all platforms: aoqi@0: // 4852688: ProfileInterpreter is off by default for ia64. The result is aoqi@0: // infinite heroic-opt-uncommon-trap/deopt/recompile cycles, since the aoqi@0: // recompile relies on a MethodData* to record heroic opt failures. aoqi@0: aoqi@0: // Whether the interpreter is producing MDO data or not, we also need aoqi@0: // to use the MDO to detect hot deoptimization points and control aoqi@0: // aggressive optimization. aoqi@0: bool inc_recompile_count = false; aoqi@0: ProfileData* pdata = NULL; aoqi@0: if (ProfileTraps && update_trap_state && trap_mdo != NULL) { aoqi@0: assert(trap_mdo == get_method_data(thread, trap_method, false), "sanity"); aoqi@0: uint this_trap_count = 0; aoqi@0: bool maybe_prior_trap = false; aoqi@0: bool maybe_prior_recompile = false; aoqi@0: pdata = query_update_method_data(trap_mdo, trap_bci, reason, aoqi@0: nm->method(), aoqi@0: //outputs: aoqi@0: this_trap_count, aoqi@0: maybe_prior_trap, aoqi@0: maybe_prior_recompile); aoqi@0: // Because the interpreter also counts null, div0, range, and class aoqi@0: // checks, these traps from compiled code are double-counted. aoqi@0: // This is harmless; it just means that the PerXTrapLimit values aoqi@0: // are in effect a little smaller than they look. aoqi@0: aoqi@0: DeoptReason per_bc_reason = reason_recorded_per_bytecode_if_any(reason); aoqi@0: if (per_bc_reason != Reason_none) { aoqi@0: // Now take action based on the partially known per-BCI history. aoqi@0: if (maybe_prior_trap aoqi@0: && this_trap_count >= (uint)PerBytecodeTrapLimit) { aoqi@0: // If there are too many traps at this BCI, force a recompile. aoqi@0: // This will allow the compiler to see the limit overflow, and aoqi@0: // take corrective action, if possible. The compiler generally aoqi@0: // does not use the exact PerBytecodeTrapLimit value, but instead aoqi@0: // changes its tactics if it sees any traps at all. This provides aoqi@0: // a little hysteresis, delaying a recompile until a trap happens aoqi@0: // several times. aoqi@0: // aoqi@0: // Actually, since there is only one bit of counter per BCI, aoqi@0: // the possible per-BCI counts are {0,1,(per-method count)}. aoqi@0: // This produces accurate results if in fact there is only aoqi@0: // one hot trap site, but begins to get fuzzy if there are aoqi@0: // many sites. For example, if there are ten sites each aoqi@0: // trapping two or more times, they each get the blame for aoqi@0: // all of their traps. aoqi@0: make_not_entrant = true; aoqi@0: } aoqi@0: aoqi@0: // Detect repeated recompilation at the same BCI, and enforce a limit. aoqi@0: if (make_not_entrant && maybe_prior_recompile) { aoqi@0: // More than one recompile at this point. aoqi@0: inc_recompile_count = maybe_prior_trap; aoqi@0: } aoqi@0: } else { aoqi@0: // For reasons which are not recorded per-bytecode, we simply aoqi@0: // force recompiles unconditionally. aoqi@0: // (Note that PerMethodRecompilationCutoff is enforced elsewhere.) aoqi@0: make_not_entrant = true; aoqi@0: } aoqi@0: aoqi@0: // Go back to the compiler if there are too many traps in this method. aoqi@0: if (this_trap_count >= per_method_trap_limit(reason)) { aoqi@0: // If there are too many traps in this method, force a recompile. aoqi@0: // This will allow the compiler to see the limit overflow, and aoqi@0: // take corrective action, if possible. aoqi@0: // (This condition is an unlikely backstop only, because the aoqi@0: // PerBytecodeTrapLimit is more likely to take effect first, aoqi@0: // if it is applicable.) aoqi@0: make_not_entrant = true; aoqi@0: } aoqi@0: aoqi@0: // Here's more hysteresis: If there has been a recompile at aoqi@0: // this trap point already, run the method in the interpreter aoqi@0: // for a while to exercise it more thoroughly. aoqi@0: if (make_not_entrant && maybe_prior_recompile && maybe_prior_trap) { aoqi@0: reprofile = true; aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: // Take requested actions on the method: aoqi@0: aoqi@0: // Recompile aoqi@0: if (make_not_entrant) { aoqi@0: if (!nm->make_not_entrant()) { aoqi@0: return; // the call did not change nmethod's state aoqi@0: } aoqi@0: aoqi@0: if (pdata != NULL) { aoqi@0: // Record the recompilation event, if any. aoqi@0: int tstate0 = pdata->trap_state(); aoqi@0: int tstate1 = trap_state_set_recompiled(tstate0, true); aoqi@0: if (tstate1 != tstate0) aoqi@0: pdata->set_trap_state(tstate1); aoqi@0: } aoqi@0: aoqi@0: #if INCLUDE_RTM_OPT aoqi@0: // Restart collecting RTM locking abort statistic if the method aoqi@0: // is recompiled for a reason other than RTM state change. aoqi@0: // Assume that in new recompiled code the statistic could be different, aoqi@0: // for example, due to different inlining. aoqi@0: if ((reason != Reason_rtm_state_change) && (trap_mdo != NULL) && aoqi@0: UseRTMDeopt && (nm->rtm_state() != ProfileRTM)) { aoqi@0: trap_mdo->atomic_set_rtm_state(ProfileRTM); aoqi@0: } aoqi@0: #endif aoqi@0: } aoqi@0: aoqi@0: if (inc_recompile_count) { aoqi@0: trap_mdo->inc_overflow_recompile_count(); aoqi@0: if ((uint)trap_mdo->overflow_recompile_count() > aoqi@0: (uint)PerBytecodeRecompilationCutoff) { aoqi@0: // Give up on the method containing the bad BCI. aoqi@0: if (trap_method() == nm->method()) { aoqi@0: make_not_compilable = true; aoqi@0: } else { aoqi@0: trap_method->set_not_compilable(CompLevel_full_optimization, true, "overflow_recompile_count > PerBytecodeRecompilationCutoff"); aoqi@0: // But give grace to the enclosing nm->method(). aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Reprofile aoqi@0: if (reprofile) { aoqi@0: CompilationPolicy::policy()->reprofile(trap_scope, nm->is_osr_method()); aoqi@0: } aoqi@0: aoqi@0: // Give up compiling aoqi@0: if (make_not_compilable && !nm->method()->is_not_compilable(CompLevel_full_optimization)) { aoqi@0: assert(make_not_entrant, "consistent"); aoqi@0: nm->method()->set_not_compilable(CompLevel_full_optimization); aoqi@0: } aoqi@0: aoqi@0: } // Free marked resources aoqi@0: aoqi@0: } aoqi@0: JRT_END aoqi@0: aoqi@0: MethodData* aoqi@0: Deoptimization::get_method_data(JavaThread* thread, methodHandle m, aoqi@0: bool create_if_missing) { aoqi@0: Thread* THREAD = thread; aoqi@0: MethodData* mdo = m()->method_data(); aoqi@0: if (mdo == NULL && create_if_missing && !HAS_PENDING_EXCEPTION) { aoqi@0: // Build an MDO. Ignore errors like OutOfMemory; aoqi@0: // that simply means we won't have an MDO to update. aoqi@0: Method::build_interpreter_method_data(m, THREAD); aoqi@0: if (HAS_PENDING_EXCEPTION) { aoqi@0: assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here"); aoqi@0: CLEAR_PENDING_EXCEPTION; aoqi@0: } aoqi@0: mdo = m()->method_data(); aoqi@0: } aoqi@0: return mdo; aoqi@0: } aoqi@0: aoqi@0: ProfileData* aoqi@0: Deoptimization::query_update_method_data(MethodData* trap_mdo, aoqi@0: int trap_bci, aoqi@0: Deoptimization::DeoptReason reason, aoqi@0: Method* compiled_method, aoqi@0: //outputs: aoqi@0: uint& ret_this_trap_count, aoqi@0: bool& ret_maybe_prior_trap, aoqi@0: bool& ret_maybe_prior_recompile) { aoqi@0: uint prior_trap_count = trap_mdo->trap_count(reason); aoqi@0: uint this_trap_count = trap_mdo->inc_trap_count(reason); aoqi@0: aoqi@0: // If the runtime cannot find a place to store trap history, aoqi@0: // it is estimated based on the general condition of the method. aoqi@0: // If the method has ever been recompiled, or has ever incurred aoqi@0: // a trap with the present reason , then this BCI is assumed aoqi@0: // (pessimistically) to be the culprit. aoqi@0: bool maybe_prior_trap = (prior_trap_count != 0); aoqi@0: bool maybe_prior_recompile = (trap_mdo->decompile_count() != 0); aoqi@0: ProfileData* pdata = NULL; aoqi@0: aoqi@0: aoqi@0: // For reasons which are recorded per bytecode, we check per-BCI data. aoqi@0: DeoptReason per_bc_reason = reason_recorded_per_bytecode_if_any(reason); aoqi@0: if (per_bc_reason != Reason_none) { aoqi@0: // Find the profile data for this BCI. If there isn't one, aoqi@0: // try to allocate one from the MDO's set of spares. aoqi@0: // This will let us detect a repeated trap at this point. aoqi@0: pdata = trap_mdo->allocate_bci_to_data(trap_bci, reason_is_speculate(reason) ? compiled_method : NULL); aoqi@0: aoqi@0: if (pdata != NULL) { aoqi@0: if (reason_is_speculate(reason) && !pdata->is_SpeculativeTrapData()) { aoqi@0: if (LogCompilation && xtty != NULL) { aoqi@0: ttyLocker ttyl; aoqi@0: // no more room for speculative traps in this MDO aoqi@0: xtty->elem("speculative_traps_oom"); aoqi@0: } aoqi@0: } aoqi@0: // Query the trap state of this profile datum. aoqi@0: int tstate0 = pdata->trap_state(); aoqi@0: if (!trap_state_has_reason(tstate0, per_bc_reason)) aoqi@0: maybe_prior_trap = false; aoqi@0: if (!trap_state_is_recompiled(tstate0)) aoqi@0: maybe_prior_recompile = false; aoqi@0: aoqi@0: // Update the trap state of this profile datum. aoqi@0: int tstate1 = tstate0; aoqi@0: // Record the reason. aoqi@0: tstate1 = trap_state_add_reason(tstate1, per_bc_reason); aoqi@0: // Store the updated state on the MDO, for next time. aoqi@0: if (tstate1 != tstate0) aoqi@0: pdata->set_trap_state(tstate1); aoqi@0: } else { aoqi@0: if (LogCompilation && xtty != NULL) { aoqi@0: ttyLocker ttyl; aoqi@0: // Missing MDP? Leave a small complaint in the log. aoqi@0: xtty->elem("missing_mdp bci='%d'", trap_bci); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Return results: aoqi@0: ret_this_trap_count = this_trap_count; aoqi@0: ret_maybe_prior_trap = maybe_prior_trap; aoqi@0: ret_maybe_prior_recompile = maybe_prior_recompile; aoqi@0: return pdata; aoqi@0: } aoqi@0: aoqi@0: void aoqi@0: Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) { aoqi@0: ResourceMark rm; aoqi@0: // Ignored outputs: aoqi@0: uint ignore_this_trap_count; aoqi@0: bool ignore_maybe_prior_trap; aoqi@0: bool ignore_maybe_prior_recompile; aoqi@0: assert(!reason_is_speculate(reason), "reason speculate only used by compiler"); aoqi@0: query_update_method_data(trap_mdo, trap_bci, aoqi@0: (DeoptReason)reason, aoqi@0: NULL, aoqi@0: ignore_this_trap_count, aoqi@0: ignore_maybe_prior_trap, aoqi@0: ignore_maybe_prior_recompile); aoqi@0: } aoqi@0: aoqi@0: Deoptimization::UnrollBlock* Deoptimization::uncommon_trap(JavaThread* thread, jint trap_request) { aoqi@0: aoqi@0: // Still in Java no safepoints aoqi@0: { aoqi@0: // This enters VM and may safepoint aoqi@0: uncommon_trap_inner(thread, trap_request); aoqi@0: } aoqi@0: return fetch_unroll_info_helper(thread); aoqi@0: } aoqi@0: aoqi@0: // Local derived constants. aoqi@0: // Further breakdown of DataLayout::trap_state, as promised by DataLayout. aoqi@0: const int DS_REASON_MASK = DataLayout::trap_mask >> 1; aoqi@0: const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK; aoqi@0: aoqi@0: //---------------------------trap_state_reason--------------------------------- aoqi@0: Deoptimization::DeoptReason aoqi@0: Deoptimization::trap_state_reason(int trap_state) { aoqi@0: // This assert provides the link between the width of DataLayout::trap_bits aoqi@0: // and the encoding of "recorded" reasons. It ensures there are enough aoqi@0: // bits to store all needed reasons in the per-BCI MDO profile. aoqi@0: assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits"); aoqi@0: int recompile_bit = (trap_state & DS_RECOMPILE_BIT); aoqi@0: trap_state -= recompile_bit; aoqi@0: if (trap_state == DS_REASON_MASK) { aoqi@0: return Reason_many; aoqi@0: } else { aoqi@0: assert((int)Reason_none == 0, "state=0 => Reason_none"); aoqi@0: return (DeoptReason)trap_state; aoqi@0: } aoqi@0: } aoqi@0: //-------------------------trap_state_has_reason------------------------------- aoqi@0: int Deoptimization::trap_state_has_reason(int trap_state, int reason) { aoqi@0: assert(reason_is_recorded_per_bytecode((DeoptReason)reason), "valid reason"); aoqi@0: assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits"); aoqi@0: int recompile_bit = (trap_state & DS_RECOMPILE_BIT); aoqi@0: trap_state -= recompile_bit; aoqi@0: if (trap_state == DS_REASON_MASK) { aoqi@0: return -1; // true, unspecifically (bottom of state lattice) aoqi@0: } else if (trap_state == reason) { aoqi@0: return 1; // true, definitely aoqi@0: } else if (trap_state == 0) { aoqi@0: return 0; // false, definitely (top of state lattice) aoqi@0: } else { aoqi@0: return 0; // false, definitely aoqi@0: } aoqi@0: } aoqi@0: //-------------------------trap_state_add_reason------------------------------- aoqi@0: int Deoptimization::trap_state_add_reason(int trap_state, int reason) { aoqi@0: assert(reason_is_recorded_per_bytecode((DeoptReason)reason) || reason == Reason_many, "valid reason"); aoqi@0: int recompile_bit = (trap_state & DS_RECOMPILE_BIT); aoqi@0: trap_state -= recompile_bit; aoqi@0: if (trap_state == DS_REASON_MASK) { aoqi@0: return trap_state + recompile_bit; // already at state lattice bottom aoqi@0: } else if (trap_state == reason) { aoqi@0: return trap_state + recompile_bit; // the condition is already true aoqi@0: } else if (trap_state == 0) { aoqi@0: return reason + recompile_bit; // no condition has yet been true aoqi@0: } else { aoqi@0: return DS_REASON_MASK + recompile_bit; // fall to state lattice bottom aoqi@0: } aoqi@0: } aoqi@0: //-----------------------trap_state_is_recompiled------------------------------ aoqi@0: bool Deoptimization::trap_state_is_recompiled(int trap_state) { aoqi@0: return (trap_state & DS_RECOMPILE_BIT) != 0; aoqi@0: } aoqi@0: //-----------------------trap_state_set_recompiled----------------------------- aoqi@0: int Deoptimization::trap_state_set_recompiled(int trap_state, bool z) { aoqi@0: if (z) return trap_state | DS_RECOMPILE_BIT; aoqi@0: else return trap_state & ~DS_RECOMPILE_BIT; aoqi@0: } aoqi@0: //---------------------------format_trap_state--------------------------------- aoqi@0: // This is used for debugging and diagnostics, including LogFile output. aoqi@0: const char* Deoptimization::format_trap_state(char* buf, size_t buflen, aoqi@0: int trap_state) { aoqi@0: DeoptReason reason = trap_state_reason(trap_state); aoqi@0: bool recomp_flag = trap_state_is_recompiled(trap_state); aoqi@0: // Re-encode the state from its decoded components. aoqi@0: int decoded_state = 0; aoqi@0: if (reason_is_recorded_per_bytecode(reason) || reason == Reason_many) aoqi@0: decoded_state = trap_state_add_reason(decoded_state, reason); aoqi@0: if (recomp_flag) aoqi@0: decoded_state = trap_state_set_recompiled(decoded_state, recomp_flag); aoqi@0: // If the state re-encodes properly, format it symbolically. aoqi@0: // Because this routine is used for debugging and diagnostics, aoqi@0: // be robust even if the state is a strange value. aoqi@0: size_t len; aoqi@0: if (decoded_state != trap_state) { aoqi@0: // Random buggy state that doesn't decode?? aoqi@0: len = jio_snprintf(buf, buflen, "#%d", trap_state); aoqi@0: } else { aoqi@0: len = jio_snprintf(buf, buflen, "%s%s", aoqi@0: trap_reason_name(reason), aoqi@0: recomp_flag ? " recompiled" : ""); aoqi@0: } aoqi@0: return buf; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: //--------------------------------statics-------------------------------------- aoqi@0: Deoptimization::DeoptAction Deoptimization::_unloaded_action aoqi@0: = Deoptimization::Action_reinterpret; aoqi@0: const char* Deoptimization::_trap_reason_name[Reason_LIMIT] = { aoqi@0: // Note: Keep this in sync. with enum DeoptReason. aoqi@0: "none", aoqi@0: "null_check", aoqi@0: "null_assert", aoqi@0: "range_check", aoqi@0: "class_check", aoqi@0: "array_check", aoqi@0: "intrinsic", aoqi@0: "bimorphic", aoqi@0: "unloaded", aoqi@0: "uninitialized", aoqi@0: "unreached", aoqi@0: "unhandled", aoqi@0: "constraint", aoqi@0: "div0_check", aoqi@0: "age", aoqi@0: "predicate", aoqi@0: "loop_limit_check", aoqi@0: "speculate_class_check", rbackman@7153: "rtm_state_change", rbackman@7153: "unstable_if" aoqi@0: }; aoqi@0: const char* Deoptimization::_trap_action_name[Action_LIMIT] = { aoqi@0: // Note: Keep this in sync. with enum DeoptAction. aoqi@0: "none", aoqi@0: "maybe_recompile", aoqi@0: "reinterpret", aoqi@0: "make_not_entrant", aoqi@0: "make_not_compilable" aoqi@0: }; aoqi@0: aoqi@0: const char* Deoptimization::trap_reason_name(int reason) { aoqi@0: if (reason == Reason_many) return "many"; aoqi@0: if ((uint)reason < Reason_LIMIT) aoqi@0: return _trap_reason_name[reason]; aoqi@0: static char buf[20]; aoqi@0: sprintf(buf, "reason%d", reason); aoqi@0: return buf; aoqi@0: } aoqi@0: const char* Deoptimization::trap_action_name(int action) { aoqi@0: if ((uint)action < Action_LIMIT) aoqi@0: return _trap_action_name[action]; aoqi@0: static char buf[20]; aoqi@0: sprintf(buf, "action%d", action); aoqi@0: return buf; aoqi@0: } aoqi@0: aoqi@0: // This is used for debugging and diagnostics, including LogFile output. aoqi@0: const char* Deoptimization::format_trap_request(char* buf, size_t buflen, aoqi@0: int trap_request) { aoqi@0: jint unloaded_class_index = trap_request_index(trap_request); aoqi@0: const char* reason = trap_reason_name(trap_request_reason(trap_request)); aoqi@0: const char* action = trap_action_name(trap_request_action(trap_request)); aoqi@0: size_t len; aoqi@0: if (unloaded_class_index < 0) { aoqi@0: len = jio_snprintf(buf, buflen, "reason='%s' action='%s'", aoqi@0: reason, action); aoqi@0: } else { aoqi@0: len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'", aoqi@0: reason, action, unloaded_class_index); aoqi@0: } aoqi@0: return buf; aoqi@0: } aoqi@0: aoqi@0: juint Deoptimization::_deoptimization_hist aoqi@0: [Deoptimization::Reason_LIMIT] aoqi@0: [1 + Deoptimization::Action_LIMIT] aoqi@0: [Deoptimization::BC_CASE_LIMIT] aoqi@0: = {0}; aoqi@0: aoqi@0: enum { aoqi@0: LSB_BITS = 8, aoqi@0: LSB_MASK = right_n_bits(LSB_BITS) aoqi@0: }; aoqi@0: aoqi@0: void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action, aoqi@0: Bytecodes::Code bc) { aoqi@0: assert(reason >= 0 && reason < Reason_LIMIT, "oob"); aoqi@0: assert(action >= 0 && action < Action_LIMIT, "oob"); aoqi@0: _deoptimization_hist[Reason_none][0][0] += 1; // total aoqi@0: _deoptimization_hist[reason][0][0] += 1; // per-reason total aoqi@0: juint* cases = _deoptimization_hist[reason][1+action]; aoqi@0: juint* bc_counter_addr = NULL; aoqi@0: juint bc_counter = 0; aoqi@0: // Look for an unused counter, or an exact match to this BC. aoqi@0: if (bc != Bytecodes::_illegal) { aoqi@0: for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) { aoqi@0: juint* counter_addr = &cases[bc_case]; aoqi@0: juint counter = *counter_addr; aoqi@0: if ((counter == 0 && bc_counter_addr == NULL) aoqi@0: || (Bytecodes::Code)(counter & LSB_MASK) == bc) { aoqi@0: // this counter is either free or is already devoted to this BC aoqi@0: bc_counter_addr = counter_addr; aoqi@0: bc_counter = counter | bc; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: if (bc_counter_addr == NULL) { aoqi@0: // Overflow, or no given bytecode. aoqi@0: bc_counter_addr = &cases[BC_CASE_LIMIT-1]; aoqi@0: bc_counter = (*bc_counter_addr & ~LSB_MASK); // clear LSB aoqi@0: } aoqi@0: *bc_counter_addr = bc_counter + (1 << LSB_BITS); aoqi@0: } aoqi@0: aoqi@0: jint Deoptimization::total_deoptimization_count() { aoqi@0: return _deoptimization_hist[Reason_none][0][0]; aoqi@0: } aoqi@0: aoqi@0: jint Deoptimization::deoptimization_count(DeoptReason reason) { aoqi@0: assert(reason >= 0 && reason < Reason_LIMIT, "oob"); aoqi@0: return _deoptimization_hist[reason][0][0]; aoqi@0: } aoqi@0: aoqi@0: void Deoptimization::print_statistics() { aoqi@0: juint total = total_deoptimization_count(); aoqi@0: juint account = total; aoqi@0: if (total != 0) { aoqi@0: ttyLocker ttyl; aoqi@0: if (xtty != NULL) xtty->head("statistics type='deoptimization'"); aoqi@0: tty->print_cr("Deoptimization traps recorded:"); aoqi@0: #define PRINT_STAT_LINE(name, r) \ aoqi@0: tty->print_cr(" %4d (%4.1f%%) %s", (int)(r), ((r) * 100.0) / total, name); aoqi@0: PRINT_STAT_LINE("total", total); aoqi@0: // For each non-zero entry in the histogram, print the reason, aoqi@0: // the action, and (if specifically known) the type of bytecode. aoqi@0: for (int reason = 0; reason < Reason_LIMIT; reason++) { aoqi@0: for (int action = 0; action < Action_LIMIT; action++) { aoqi@0: juint* cases = _deoptimization_hist[reason][1+action]; aoqi@0: for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) { aoqi@0: juint counter = cases[bc_case]; aoqi@0: if (counter != 0) { aoqi@0: char name[1*K]; aoqi@0: Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK); aoqi@0: if (bc_case == BC_CASE_LIMIT && (int)bc == 0) aoqi@0: bc = Bytecodes::_illegal; aoqi@0: sprintf(name, "%s/%s/%s", aoqi@0: trap_reason_name(reason), aoqi@0: trap_action_name(action), aoqi@0: Bytecodes::is_defined(bc)? Bytecodes::name(bc): "other"); aoqi@0: juint r = counter >> LSB_BITS; aoqi@0: tty->print_cr(" %40s: " UINT32_FORMAT " (%.1f%%)", name, r, (r * 100.0) / total); aoqi@0: account -= r; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: if (account != 0) { aoqi@0: PRINT_STAT_LINE("unaccounted", account); aoqi@0: } aoqi@0: #undef PRINT_STAT_LINE aoqi@0: if (xtty != NULL) xtty->tail("statistics"); aoqi@0: } aoqi@0: } aoqi@0: #else // COMPILER2 || SHARK aoqi@0: aoqi@0: aoqi@0: // Stubs for C1 only system. aoqi@0: bool Deoptimization::trap_state_is_recompiled(int trap_state) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: const char* Deoptimization::trap_reason_name(int reason) { aoqi@0: return "unknown"; aoqi@0: } aoqi@0: aoqi@0: void Deoptimization::print_statistics() { aoqi@0: // no output aoqi@0: } aoqi@0: aoqi@0: void aoqi@0: Deoptimization::update_method_data_from_interpreter(MethodData* trap_mdo, int trap_bci, int reason) { aoqi@0: // no udpate aoqi@0: } aoqi@0: aoqi@0: int Deoptimization::trap_state_has_reason(int trap_state, int reason) { aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action, aoqi@0: Bytecodes::Code bc) { aoqi@0: // no update aoqi@0: } aoqi@0: aoqi@0: const char* Deoptimization::format_trap_state(char* buf, size_t buflen, aoqi@0: int trap_state) { aoqi@0: jio_snprintf(buf, buflen, "#%d", trap_state); aoqi@0: return buf; aoqi@0: } aoqi@0: aoqi@0: #endif // COMPILER2 || SHARK