1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/runtime/deoptimization.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,1789 @@ 1.4 +/* 1.5 + * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "incls/_precompiled.incl" 1.29 +#include "incls/_deoptimization.cpp.incl" 1.30 + 1.31 +bool DeoptimizationMarker::_is_active = false; 1.32 + 1.33 +Deoptimization::UnrollBlock::UnrollBlock(int size_of_deoptimized_frame, 1.34 + int caller_adjustment, 1.35 + int number_of_frames, 1.36 + intptr_t* frame_sizes, 1.37 + address* frame_pcs, 1.38 + BasicType return_type) { 1.39 + _size_of_deoptimized_frame = size_of_deoptimized_frame; 1.40 + _caller_adjustment = caller_adjustment; 1.41 + _number_of_frames = number_of_frames; 1.42 + _frame_sizes = frame_sizes; 1.43 + _frame_pcs = frame_pcs; 1.44 + _register_block = NEW_C_HEAP_ARRAY(intptr_t, RegisterMap::reg_count * 2); 1.45 + _return_type = return_type; 1.46 + // PD (x86 only) 1.47 + _counter_temp = 0; 1.48 + _initial_fp = 0; 1.49 + _unpack_kind = 0; 1.50 + _sender_sp_temp = 0; 1.51 + 1.52 + _total_frame_sizes = size_of_frames(); 1.53 +} 1.54 + 1.55 + 1.56 +Deoptimization::UnrollBlock::~UnrollBlock() { 1.57 + FREE_C_HEAP_ARRAY(intptr_t, _frame_sizes); 1.58 + FREE_C_HEAP_ARRAY(intptr_t, _frame_pcs); 1.59 + FREE_C_HEAP_ARRAY(intptr_t, _register_block); 1.60 +} 1.61 + 1.62 + 1.63 +intptr_t* Deoptimization::UnrollBlock::value_addr_at(int register_number) const { 1.64 + assert(register_number < RegisterMap::reg_count, "checking register number"); 1.65 + return &_register_block[register_number * 2]; 1.66 +} 1.67 + 1.68 + 1.69 + 1.70 +int Deoptimization::UnrollBlock::size_of_frames() const { 1.71 + // Acount first for the adjustment of the initial frame 1.72 + int result = _caller_adjustment; 1.73 + for (int index = 0; index < number_of_frames(); index++) { 1.74 + result += frame_sizes()[index]; 1.75 + } 1.76 + return result; 1.77 +} 1.78 + 1.79 + 1.80 +void Deoptimization::UnrollBlock::print() { 1.81 + ttyLocker ttyl; 1.82 + tty->print_cr("UnrollBlock"); 1.83 + tty->print_cr(" size_of_deoptimized_frame = %d", _size_of_deoptimized_frame); 1.84 + tty->print( " frame_sizes: "); 1.85 + for (int index = 0; index < number_of_frames(); index++) { 1.86 + tty->print("%d ", frame_sizes()[index]); 1.87 + } 1.88 + tty->cr(); 1.89 +} 1.90 + 1.91 + 1.92 +// In order to make fetch_unroll_info work properly with escape 1.93 +// analysis, The method was changed from JRT_LEAF to JRT_BLOCK_ENTRY and 1.94 +// ResetNoHandleMark and HandleMark were removed from it. The actual reallocation 1.95 +// of previously eliminated objects occurs in realloc_objects, which is 1.96 +// called from the method fetch_unroll_info_helper below. 1.97 +JRT_BLOCK_ENTRY(Deoptimization::UnrollBlock*, Deoptimization::fetch_unroll_info(JavaThread* thread)) 1.98 + // It is actually ok to allocate handles in a leaf method. It causes no safepoints, 1.99 + // but makes the entry a little slower. There is however a little dance we have to 1.100 + // do in debug mode to get around the NoHandleMark code in the JRT_LEAF macro 1.101 + 1.102 + // fetch_unroll_info() is called at the beginning of the deoptimization 1.103 + // handler. Note this fact before we start generating temporary frames 1.104 + // that can confuse an asynchronous stack walker. This counter is 1.105 + // decremented at the end of unpack_frames(). 1.106 + thread->inc_in_deopt_handler(); 1.107 + 1.108 + return fetch_unroll_info_helper(thread); 1.109 +JRT_END 1.110 + 1.111 + 1.112 +// This is factored, since it is both called from a JRT_LEAF (deoptimization) and a JRT_ENTRY (uncommon_trap) 1.113 +Deoptimization::UnrollBlock* Deoptimization::fetch_unroll_info_helper(JavaThread* thread) { 1.114 + 1.115 + // Note: there is a safepoint safety issue here. No matter whether we enter 1.116 + // via vanilla deopt or uncommon trap we MUST NOT stop at a safepoint once 1.117 + // the vframeArray is created. 1.118 + // 1.119 + 1.120 + // Allocate our special deoptimization ResourceMark 1.121 + DeoptResourceMark* dmark = new DeoptResourceMark(thread); 1.122 + assert(thread->deopt_mark() == NULL, "Pending deopt!"); 1.123 + thread->set_deopt_mark(dmark); 1.124 + 1.125 + frame stub_frame = thread->last_frame(); // Makes stack walkable as side effect 1.126 + RegisterMap map(thread, true); 1.127 + RegisterMap dummy_map(thread, false); 1.128 + // Now get the deoptee with a valid map 1.129 + frame deoptee = stub_frame.sender(&map); 1.130 + 1.131 + // Create a growable array of VFrames where each VFrame represents an inlined 1.132 + // Java frame. This storage is allocated with the usual system arena. 1.133 + assert(deoptee.is_compiled_frame(), "Wrong frame type"); 1.134 + GrowableArray<compiledVFrame*>* chunk = new GrowableArray<compiledVFrame*>(10); 1.135 + vframe* vf = vframe::new_vframe(&deoptee, &map, thread); 1.136 + while (!vf->is_top()) { 1.137 + assert(vf->is_compiled_frame(), "Wrong frame type"); 1.138 + chunk->push(compiledVFrame::cast(vf)); 1.139 + vf = vf->sender(); 1.140 + } 1.141 + assert(vf->is_compiled_frame(), "Wrong frame type"); 1.142 + chunk->push(compiledVFrame::cast(vf)); 1.143 + 1.144 +#ifdef COMPILER2 1.145 + // Reallocate the non-escaping objects and restore their fields. Then 1.146 + // relock objects if synchronization on them was eliminated. 1.147 + if (DoEscapeAnalysis && EliminateAllocations) { 1.148 + GrowableArray<ScopeValue*>* objects = chunk->at(0)->scope()->objects(); 1.149 + bool reallocated = false; 1.150 + if (objects != NULL) { 1.151 + JRT_BLOCK 1.152 + reallocated = realloc_objects(thread, &deoptee, objects, THREAD); 1.153 + JRT_END 1.154 + } 1.155 + if (reallocated) { 1.156 + reassign_fields(&deoptee, &map, objects); 1.157 +#ifndef PRODUCT 1.158 + if (TraceDeoptimization) { 1.159 + ttyLocker ttyl; 1.160 + tty->print_cr("REALLOC OBJECTS in thread " INTPTR_FORMAT, thread); 1.161 + print_objects(objects); 1.162 + } 1.163 +#endif 1.164 + } 1.165 + for (int i = 0; i < chunk->length(); i++) { 1.166 + GrowableArray<MonitorValue*>* monitors = chunk->at(i)->scope()->monitors(); 1.167 + if (monitors != NULL) { 1.168 + relock_objects(&deoptee, &map, monitors); 1.169 +#ifndef PRODUCT 1.170 + if (TraceDeoptimization) { 1.171 + ttyLocker ttyl; 1.172 + tty->print_cr("RELOCK OBJECTS in thread " INTPTR_FORMAT, thread); 1.173 + for (int j = 0; i < monitors->length(); i++) { 1.174 + MonitorValue* mv = monitors->at(i); 1.175 + if (mv->eliminated()) { 1.176 + StackValue* owner = StackValue::create_stack_value(&deoptee, &map, mv->owner()); 1.177 + tty->print_cr(" object <" INTPTR_FORMAT "> locked", owner->get_obj()()); 1.178 + } 1.179 + } 1.180 + } 1.181 +#endif 1.182 + } 1.183 + } 1.184 + } 1.185 +#endif // COMPILER2 1.186 + // Ensure that no safepoint is taken after pointers have been stored 1.187 + // in fields of rematerialized objects. If a safepoint occurs from here on 1.188 + // out the java state residing in the vframeArray will be missed. 1.189 + No_Safepoint_Verifier no_safepoint; 1.190 + 1.191 + vframeArray* array = create_vframeArray(thread, deoptee, &map, chunk); 1.192 + 1.193 + assert(thread->vframe_array_head() == NULL, "Pending deopt!");; 1.194 + thread->set_vframe_array_head(array); 1.195 + 1.196 + // Now that the vframeArray has been created if we have any deferred local writes 1.197 + // added by jvmti then we can free up that structure as the data is now in the 1.198 + // vframeArray 1.199 + 1.200 + if (thread->deferred_locals() != NULL) { 1.201 + GrowableArray<jvmtiDeferredLocalVariableSet*>* list = thread->deferred_locals(); 1.202 + int i = 0; 1.203 + do { 1.204 + // Because of inlining we could have multiple vframes for a single frame 1.205 + // and several of the vframes could have deferred writes. Find them all. 1.206 + if (list->at(i)->id() == array->original().id()) { 1.207 + jvmtiDeferredLocalVariableSet* dlv = list->at(i); 1.208 + list->remove_at(i); 1.209 + // individual jvmtiDeferredLocalVariableSet are CHeapObj's 1.210 + delete dlv; 1.211 + } else { 1.212 + i++; 1.213 + } 1.214 + } while ( i < list->length() ); 1.215 + if (list->length() == 0) { 1.216 + thread->set_deferred_locals(NULL); 1.217 + // free the list and elements back to C heap. 1.218 + delete list; 1.219 + } 1.220 + 1.221 + } 1.222 + 1.223 + // Compute the caller frame based on the sender sp of stub_frame and stored frame sizes info. 1.224 + CodeBlob* cb = stub_frame.cb(); 1.225 + // Verify we have the right vframeArray 1.226 + assert(cb->frame_size() >= 0, "Unexpected frame size"); 1.227 + intptr_t* unpack_sp = stub_frame.sp() + cb->frame_size(); 1.228 + 1.229 +#ifdef ASSERT 1.230 + assert(cb->is_deoptimization_stub() || cb->is_uncommon_trap_stub(), "just checking"); 1.231 + Events::log("fetch unroll sp " INTPTR_FORMAT, unpack_sp); 1.232 +#endif 1.233 + // This is a guarantee instead of an assert because if vframe doesn't match 1.234 + // we will unpack the wrong deoptimized frame and wind up in strange places 1.235 + // where it will be very difficult to figure out what went wrong. Better 1.236 + // to die an early death here than some very obscure death later when the 1.237 + // trail is cold. 1.238 + // Note: on ia64 this guarantee can be fooled by frames with no memory stack 1.239 + // in that it will fail to detect a problem when there is one. This needs 1.240 + // more work in tiger timeframe. 1.241 + guarantee(array->unextended_sp() == unpack_sp, "vframe_array_head must contain the vframeArray to unpack"); 1.242 + 1.243 + int number_of_frames = array->frames(); 1.244 + 1.245 + // Compute the vframes' sizes. Note that frame_sizes[] entries are ordered from outermost to innermost 1.246 + // virtual activation, which is the reverse of the elements in the vframes array. 1.247 + intptr_t* frame_sizes = NEW_C_HEAP_ARRAY(intptr_t, number_of_frames); 1.248 + // +1 because we always have an interpreter return address for the final slot. 1.249 + address* frame_pcs = NEW_C_HEAP_ARRAY(address, number_of_frames + 1); 1.250 + int callee_parameters = 0; 1.251 + int callee_locals = 0; 1.252 + int popframe_extra_args = 0; 1.253 + // Create an interpreter return address for the stub to use as its return 1.254 + // address so the skeletal frames are perfectly walkable 1.255 + frame_pcs[number_of_frames] = Interpreter::deopt_entry(vtos, 0); 1.256 + 1.257 + // PopFrame requires that the preserved incoming arguments from the recently-popped topmost 1.258 + // activation be put back on the expression stack of the caller for reexecution 1.259 + if (JvmtiExport::can_pop_frame() && thread->popframe_forcing_deopt_reexecution()) { 1.260 + popframe_extra_args = in_words(thread->popframe_preserved_args_size_in_words()); 1.261 + } 1.262 + 1.263 + // 1.264 + // frame_sizes/frame_pcs[0] oldest frame (int or c2i) 1.265 + // frame_sizes/frame_pcs[1] next oldest frame (int) 1.266 + // frame_sizes/frame_pcs[n] youngest frame (int) 1.267 + // 1.268 + // Now a pc in frame_pcs is actually the return address to the frame's caller (a frame 1.269 + // owns the space for the return address to it's caller). Confusing ain't it. 1.270 + // 1.271 + // The vframe array can address vframes with indices running from 1.272 + // 0.._frames-1. Index 0 is the youngest frame and _frame - 1 is the oldest (root) frame. 1.273 + // When we create the skeletal frames we need the oldest frame to be in the zero slot 1.274 + // in the frame_sizes/frame_pcs so the assembly code can do a trivial walk. 1.275 + // so things look a little strange in this loop. 1.276 + // 1.277 + for (int index = 0; index < array->frames(); index++ ) { 1.278 + // frame[number_of_frames - 1 ] = on_stack_size(youngest) 1.279 + // frame[number_of_frames - 2 ] = on_stack_size(sender(youngest)) 1.280 + // frame[number_of_frames - 3 ] = on_stack_size(sender(sender(youngest))) 1.281 + frame_sizes[number_of_frames - 1 - index] = BytesPerWord * array->element(index)->on_stack_size(callee_parameters, 1.282 + callee_locals, 1.283 + index == 0, 1.284 + popframe_extra_args); 1.285 + // This pc doesn't have to be perfect just good enough to identify the frame 1.286 + // as interpreted so the skeleton frame will be walkable 1.287 + // The correct pc will be set when the skeleton frame is completely filled out 1.288 + // The final pc we store in the loop is wrong and will be overwritten below 1.289 + frame_pcs[number_of_frames - 1 - index ] = Interpreter::deopt_entry(vtos, 0) - frame::pc_return_offset; 1.290 + 1.291 + callee_parameters = array->element(index)->method()->size_of_parameters(); 1.292 + callee_locals = array->element(index)->method()->max_locals(); 1.293 + popframe_extra_args = 0; 1.294 + } 1.295 + 1.296 + // Compute whether the root vframe returns a float or double value. 1.297 + BasicType return_type; 1.298 + { 1.299 + HandleMark hm; 1.300 + methodHandle method(thread, array->element(0)->method()); 1.301 + Bytecode_invoke* invoke = Bytecode_invoke_at_check(method, array->element(0)->bci()); 1.302 + return_type = (invoke != NULL) ? invoke->result_type(thread) : T_ILLEGAL; 1.303 + } 1.304 + 1.305 + // Compute information for handling adapters and adjusting the frame size of the caller. 1.306 + int caller_adjustment = 0; 1.307 + 1.308 + // Find the current pc for sender of the deoptee. Since the sender may have been deoptimized 1.309 + // itself since the deoptee vframeArray was created we must get a fresh value of the pc rather 1.310 + // than simply use array->sender.pc(). This requires us to walk the current set of frames 1.311 + // 1.312 + frame deopt_sender = stub_frame.sender(&dummy_map); // First is the deoptee frame 1.313 + deopt_sender = deopt_sender.sender(&dummy_map); // Now deoptee caller 1.314 + 1.315 + // Compute the amount the oldest interpreter frame will have to adjust 1.316 + // its caller's stack by. If the caller is a compiled frame then 1.317 + // we pretend that the callee has no parameters so that the 1.318 + // extension counts for the full amount of locals and not just 1.319 + // locals-parms. This is because without a c2i adapter the parm 1.320 + // area as created by the compiled frame will not be usable by 1.321 + // the interpreter. (Depending on the calling convention there 1.322 + // may not even be enough space). 1.323 + 1.324 + // QQQ I'd rather see this pushed down into last_frame_adjust 1.325 + // and have it take the sender (aka caller). 1.326 + 1.327 + if (deopt_sender.is_compiled_frame()) { 1.328 + caller_adjustment = last_frame_adjust(0, callee_locals); 1.329 + } else if (callee_locals > callee_parameters) { 1.330 + // The caller frame may need extending to accommodate 1.331 + // non-parameter locals of the first unpacked interpreted frame. 1.332 + // Compute that adjustment. 1.333 + caller_adjustment = last_frame_adjust(callee_parameters, callee_locals); 1.334 + } 1.335 + 1.336 + 1.337 + // If the sender is deoptimized the we must retrieve the address of the handler 1.338 + // since the frame will "magically" show the original pc before the deopt 1.339 + // and we'd undo the deopt. 1.340 + 1.341 + frame_pcs[0] = deopt_sender.raw_pc(); 1.342 + 1.343 + assert(CodeCache::find_blob_unsafe(frame_pcs[0]) != NULL, "bad pc"); 1.344 + 1.345 + UnrollBlock* info = new UnrollBlock(array->frame_size() * BytesPerWord, 1.346 + caller_adjustment * BytesPerWord, 1.347 + number_of_frames, 1.348 + frame_sizes, 1.349 + frame_pcs, 1.350 + return_type); 1.351 +#if defined(IA32) || defined(AMD64) 1.352 + // We need a way to pass fp to the unpacking code so the skeletal frames 1.353 + // come out correct. This is only needed for x86 because of c2 using ebp 1.354 + // as an allocatable register. So this update is useless (and harmless) 1.355 + // on the other platforms. It would be nice to do this in a different 1.356 + // way but even the old style deoptimization had a problem with deriving 1.357 + // this value. NEEDS_CLEANUP 1.358 + // Note: now that c1 is using c2's deopt blob we must do this on all 1.359 + // x86 based platforms 1.360 + intptr_t** fp_addr = (intptr_t**) (((address)info) + info->initial_fp_offset_in_bytes()); 1.361 + *fp_addr = array->sender().fp(); // was adapter_caller 1.362 +#endif /* IA32 || AMD64 */ 1.363 + 1.364 + if (array->frames() > 1) { 1.365 + if (VerifyStack && TraceDeoptimization) { 1.366 + tty->print_cr("Deoptimizing method containing inlining"); 1.367 + } 1.368 + } 1.369 + 1.370 + array->set_unroll_block(info); 1.371 + return info; 1.372 +} 1.373 + 1.374 +// Called to cleanup deoptimization data structures in normal case 1.375 +// after unpacking to stack and when stack overflow error occurs 1.376 +void Deoptimization::cleanup_deopt_info(JavaThread *thread, 1.377 + vframeArray *array) { 1.378 + 1.379 + // Get array if coming from exception 1.380 + if (array == NULL) { 1.381 + array = thread->vframe_array_head(); 1.382 + } 1.383 + thread->set_vframe_array_head(NULL); 1.384 + 1.385 + // Free the previous UnrollBlock 1.386 + vframeArray* old_array = thread->vframe_array_last(); 1.387 + thread->set_vframe_array_last(array); 1.388 + 1.389 + if (old_array != NULL) { 1.390 + UnrollBlock* old_info = old_array->unroll_block(); 1.391 + old_array->set_unroll_block(NULL); 1.392 + delete old_info; 1.393 + delete old_array; 1.394 + } 1.395 + 1.396 + // Deallocate any resource creating in this routine and any ResourceObjs allocated 1.397 + // inside the vframeArray (StackValueCollections) 1.398 + 1.399 + delete thread->deopt_mark(); 1.400 + thread->set_deopt_mark(NULL); 1.401 + 1.402 + 1.403 + if (JvmtiExport::can_pop_frame()) { 1.404 +#ifndef CC_INTERP 1.405 + // Regardless of whether we entered this routine with the pending 1.406 + // popframe condition bit set, we should always clear it now 1.407 + thread->clear_popframe_condition(); 1.408 +#else 1.409 + // C++ interpeter will clear has_pending_popframe when it enters 1.410 + // with method_resume. For deopt_resume2 we clear it now. 1.411 + if (thread->popframe_forcing_deopt_reexecution()) 1.412 + thread->clear_popframe_condition(); 1.413 +#endif /* CC_INTERP */ 1.414 + } 1.415 + 1.416 + // unpack_frames() is called at the end of the deoptimization handler 1.417 + // and (in C2) at the end of the uncommon trap handler. Note this fact 1.418 + // so that an asynchronous stack walker can work again. This counter is 1.419 + // incremented at the beginning of fetch_unroll_info() and (in C2) at 1.420 + // the beginning of uncommon_trap(). 1.421 + thread->dec_in_deopt_handler(); 1.422 +} 1.423 + 1.424 + 1.425 +// Return BasicType of value being returned 1.426 +JRT_LEAF(BasicType, Deoptimization::unpack_frames(JavaThread* thread, int exec_mode)) 1.427 + 1.428 + // We are already active int he special DeoptResourceMark any ResourceObj's we 1.429 + // allocate will be freed at the end of the routine. 1.430 + 1.431 + // It is actually ok to allocate handles in a leaf method. It causes no safepoints, 1.432 + // but makes the entry a little slower. There is however a little dance we have to 1.433 + // do in debug mode to get around the NoHandleMark code in the JRT_LEAF macro 1.434 + ResetNoHandleMark rnhm; // No-op in release/product versions 1.435 + HandleMark hm; 1.436 + 1.437 + frame stub_frame = thread->last_frame(); 1.438 + 1.439 + // Since the frame to unpack is the top frame of this thread, the vframe_array_head 1.440 + // must point to the vframeArray for the unpack frame. 1.441 + vframeArray* array = thread->vframe_array_head(); 1.442 + 1.443 +#ifndef PRODUCT 1.444 + if (TraceDeoptimization) { 1.445 + tty->print_cr("DEOPT UNPACKING thread " INTPTR_FORMAT " vframeArray " INTPTR_FORMAT " mode %d", thread, array, exec_mode); 1.446 + } 1.447 +#endif 1.448 + 1.449 + UnrollBlock* info = array->unroll_block(); 1.450 + 1.451 + // Unpack the interpreter frames and any adapter frame (c2 only) we might create. 1.452 + array->unpack_to_stack(stub_frame, exec_mode); 1.453 + 1.454 + BasicType bt = info->return_type(); 1.455 + 1.456 + // If we have an exception pending, claim that the return type is an oop 1.457 + // so the deopt_blob does not overwrite the exception_oop. 1.458 + 1.459 + if (exec_mode == Unpack_exception) 1.460 + bt = T_OBJECT; 1.461 + 1.462 + // Cleanup thread deopt data 1.463 + cleanup_deopt_info(thread, array); 1.464 + 1.465 +#ifndef PRODUCT 1.466 + if (VerifyStack) { 1.467 + ResourceMark res_mark; 1.468 + 1.469 + // Verify that the just-unpacked frames match the interpreter's 1.470 + // notions of expression stack and locals 1.471 + vframeArray* cur_array = thread->vframe_array_last(); 1.472 + RegisterMap rm(thread, false); 1.473 + rm.set_include_argument_oops(false); 1.474 + bool is_top_frame = true; 1.475 + int callee_size_of_parameters = 0; 1.476 + int callee_max_locals = 0; 1.477 + for (int i = 0; i < cur_array->frames(); i++) { 1.478 + vframeArrayElement* el = cur_array->element(i); 1.479 + frame* iframe = el->iframe(); 1.480 + guarantee(iframe->is_interpreted_frame(), "Wrong frame type"); 1.481 + 1.482 + // Get the oop map for this bci 1.483 + InterpreterOopMap mask; 1.484 + int cur_invoke_parameter_size = 0; 1.485 + bool try_next_mask = false; 1.486 + int next_mask_expression_stack_size = -1; 1.487 + int top_frame_expression_stack_adjustment = 0; 1.488 + methodHandle mh(thread, iframe->interpreter_frame_method()); 1.489 + OopMapCache::compute_one_oop_map(mh, iframe->interpreter_frame_bci(), &mask); 1.490 + BytecodeStream str(mh); 1.491 + str.set_start(iframe->interpreter_frame_bci()); 1.492 + int max_bci = mh->code_size(); 1.493 + // Get to the next bytecode if possible 1.494 + assert(str.bci() < max_bci, "bci in interpreter frame out of bounds"); 1.495 + // Check to see if we can grab the number of outgoing arguments 1.496 + // at an uncommon trap for an invoke (where the compiler 1.497 + // generates debug info before the invoke has executed) 1.498 + Bytecodes::Code cur_code = str.next(); 1.499 + if (cur_code == Bytecodes::_invokevirtual || 1.500 + cur_code == Bytecodes::_invokespecial || 1.501 + cur_code == Bytecodes::_invokestatic || 1.502 + cur_code == Bytecodes::_invokeinterface) { 1.503 + Bytecode_invoke* invoke = Bytecode_invoke_at(mh, iframe->interpreter_frame_bci()); 1.504 + symbolHandle signature(thread, invoke->signature()); 1.505 + ArgumentSizeComputer asc(signature); 1.506 + cur_invoke_parameter_size = asc.size(); 1.507 + if (cur_code != Bytecodes::_invokestatic) { 1.508 + // Add in receiver 1.509 + ++cur_invoke_parameter_size; 1.510 + } 1.511 + } 1.512 + if (str.bci() < max_bci) { 1.513 + Bytecodes::Code bc = str.next(); 1.514 + if (bc >= 0) { 1.515 + // The interpreter oop map generator reports results before 1.516 + // the current bytecode has executed except in the case of 1.517 + // calls. It seems to be hard to tell whether the compiler 1.518 + // has emitted debug information matching the "state before" 1.519 + // a given bytecode or the state after, so we try both 1.520 + switch (cur_code) { 1.521 + case Bytecodes::_invokevirtual: 1.522 + case Bytecodes::_invokespecial: 1.523 + case Bytecodes::_invokestatic: 1.524 + case Bytecodes::_invokeinterface: 1.525 + case Bytecodes::_athrow: 1.526 + break; 1.527 + default: { 1.528 + InterpreterOopMap next_mask; 1.529 + OopMapCache::compute_one_oop_map(mh, str.bci(), &next_mask); 1.530 + next_mask_expression_stack_size = next_mask.expression_stack_size(); 1.531 + // Need to subtract off the size of the result type of 1.532 + // the bytecode because this is not described in the 1.533 + // debug info but returned to the interpreter in the TOS 1.534 + // caching register 1.535 + BasicType bytecode_result_type = Bytecodes::result_type(cur_code); 1.536 + if (bytecode_result_type != T_ILLEGAL) { 1.537 + top_frame_expression_stack_adjustment = type2size[bytecode_result_type]; 1.538 + } 1.539 + assert(top_frame_expression_stack_adjustment >= 0, ""); 1.540 + try_next_mask = true; 1.541 + break; 1.542 + } 1.543 + } 1.544 + } 1.545 + } 1.546 + 1.547 + // Verify stack depth and oops in frame 1.548 + // This assertion may be dependent on the platform we're running on and may need modification (tested on x86 and sparc) 1.549 + if (!( 1.550 + /* SPARC */ 1.551 + (iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + callee_size_of_parameters) || 1.552 + /* x86 */ 1.553 + (iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + callee_max_locals) || 1.554 + (try_next_mask && 1.555 + (iframe->interpreter_frame_expression_stack_size() == (next_mask_expression_stack_size - 1.556 + top_frame_expression_stack_adjustment))) || 1.557 + (is_top_frame && (exec_mode == Unpack_exception) && iframe->interpreter_frame_expression_stack_size() == 0) || 1.558 + (is_top_frame && (exec_mode == Unpack_uncommon_trap || exec_mode == Unpack_reexecute) && 1.559 + (iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + cur_invoke_parameter_size)) 1.560 + )) { 1.561 + ttyLocker ttyl; 1.562 + 1.563 + // Print out some information that will help us debug the problem 1.564 + tty->print_cr("Wrong number of expression stack elements during deoptimization"); 1.565 + tty->print_cr(" Error occurred while verifying frame %d (0..%d, 0 is topmost)", i, cur_array->frames() - 1); 1.566 + tty->print_cr(" Fabricated interpreter frame had %d expression stack elements", 1.567 + iframe->interpreter_frame_expression_stack_size()); 1.568 + tty->print_cr(" Interpreter oop map had %d expression stack elements", mask.expression_stack_size()); 1.569 + tty->print_cr(" try_next_mask = %d", try_next_mask); 1.570 + tty->print_cr(" next_mask_expression_stack_size = %d", next_mask_expression_stack_size); 1.571 + tty->print_cr(" callee_size_of_parameters = %d", callee_size_of_parameters); 1.572 + tty->print_cr(" callee_max_locals = %d", callee_max_locals); 1.573 + tty->print_cr(" top_frame_expression_stack_adjustment = %d", top_frame_expression_stack_adjustment); 1.574 + tty->print_cr(" exec_mode = %d", exec_mode); 1.575 + tty->print_cr(" cur_invoke_parameter_size = %d", cur_invoke_parameter_size); 1.576 + tty->print_cr(" Thread = " INTPTR_FORMAT ", thread ID = " UINTX_FORMAT, thread, thread->osthread()->thread_id()); 1.577 + tty->print_cr(" Interpreted frames:"); 1.578 + for (int k = 0; k < cur_array->frames(); k++) { 1.579 + vframeArrayElement* el = cur_array->element(k); 1.580 + tty->print_cr(" %s (bci %d)", el->method()->name_and_sig_as_C_string(), el->bci()); 1.581 + } 1.582 + cur_array->print_on_2(tty); 1.583 + guarantee(false, "wrong number of expression stack elements during deopt"); 1.584 + } 1.585 + VerifyOopClosure verify; 1.586 + iframe->oops_interpreted_do(&verify, &rm, false); 1.587 + callee_size_of_parameters = mh->size_of_parameters(); 1.588 + callee_max_locals = mh->max_locals(); 1.589 + is_top_frame = false; 1.590 + } 1.591 + } 1.592 +#endif /* !PRODUCT */ 1.593 + 1.594 + 1.595 + return bt; 1.596 +JRT_END 1.597 + 1.598 + 1.599 +int Deoptimization::deoptimize_dependents() { 1.600 + Threads::deoptimized_wrt_marked_nmethods(); 1.601 + return 0; 1.602 +} 1.603 + 1.604 + 1.605 +#ifdef COMPILER2 1.606 +bool Deoptimization::realloc_objects(JavaThread* thread, frame* fr, GrowableArray<ScopeValue*>* objects, TRAPS) { 1.607 + Handle pending_exception(thread->pending_exception()); 1.608 + const char* exception_file = thread->exception_file(); 1.609 + int exception_line = thread->exception_line(); 1.610 + thread->clear_pending_exception(); 1.611 + 1.612 + for (int i = 0; i < objects->length(); i++) { 1.613 + assert(objects->at(i)->is_object(), "invalid debug information"); 1.614 + ObjectValue* sv = (ObjectValue*) objects->at(i); 1.615 + 1.616 + KlassHandle k(((ConstantOopReadValue*) sv->klass())->value()()); 1.617 + oop obj = NULL; 1.618 + 1.619 + if (k->oop_is_instance()) { 1.620 + instanceKlass* ik = instanceKlass::cast(k()); 1.621 + obj = ik->allocate_instance(CHECK_(false)); 1.622 + } else if (k->oop_is_typeArray()) { 1.623 + typeArrayKlass* ak = typeArrayKlass::cast(k()); 1.624 + assert(sv->field_size() % type2size[ak->element_type()] == 0, "non-integral array length"); 1.625 + int len = sv->field_size() / type2size[ak->element_type()]; 1.626 + obj = ak->allocate(len, CHECK_(false)); 1.627 + } else if (k->oop_is_objArray()) { 1.628 + objArrayKlass* ak = objArrayKlass::cast(k()); 1.629 + obj = ak->allocate(sv->field_size(), CHECK_(false)); 1.630 + } 1.631 + 1.632 + assert(obj != NULL, "allocation failed"); 1.633 + assert(sv->value().is_null(), "redundant reallocation"); 1.634 + sv->set_value(obj); 1.635 + } 1.636 + 1.637 + if (pending_exception.not_null()) { 1.638 + thread->set_pending_exception(pending_exception(), exception_file, exception_line); 1.639 + } 1.640 + 1.641 + return true; 1.642 +} 1.643 + 1.644 +// This assumes that the fields are stored in ObjectValue in the same order 1.645 +// they are yielded by do_nonstatic_fields. 1.646 +class FieldReassigner: public FieldClosure { 1.647 + frame* _fr; 1.648 + RegisterMap* _reg_map; 1.649 + ObjectValue* _sv; 1.650 + instanceKlass* _ik; 1.651 + oop _obj; 1.652 + 1.653 + int _i; 1.654 +public: 1.655 + FieldReassigner(frame* fr, RegisterMap* reg_map, ObjectValue* sv, oop obj) : 1.656 + _fr(fr), _reg_map(reg_map), _sv(sv), _obj(obj), _i(0) {} 1.657 + 1.658 + int i() const { return _i; } 1.659 + 1.660 + 1.661 + void do_field(fieldDescriptor* fd) { 1.662 + StackValue* value = 1.663 + StackValue::create_stack_value(_fr, _reg_map, _sv->field_at(i())); 1.664 + int offset = fd->offset(); 1.665 + switch (fd->field_type()) { 1.666 + case T_OBJECT: case T_ARRAY: 1.667 + assert(value->type() == T_OBJECT, "Agreement."); 1.668 + _obj->obj_field_put(offset, value->get_obj()()); 1.669 + break; 1.670 + 1.671 + case T_LONG: case T_DOUBLE: { 1.672 + assert(value->type() == T_INT, "Agreement."); 1.673 + StackValue* low = 1.674 + StackValue::create_stack_value(_fr, _reg_map, _sv->field_at(++_i)); 1.675 + jlong res = jlong_from((jint)value->get_int(), (jint)low->get_int()); 1.676 + _obj->long_field_put(offset, res); 1.677 + break; 1.678 + } 1.679 + 1.680 + case T_INT: case T_FLOAT: // 4 bytes. 1.681 + assert(value->type() == T_INT, "Agreement."); 1.682 + _obj->int_field_put(offset, (jint)value->get_int()); 1.683 + break; 1.684 + 1.685 + case T_SHORT: case T_CHAR: // 2 bytes 1.686 + assert(value->type() == T_INT, "Agreement."); 1.687 + _obj->short_field_put(offset, (jshort)value->get_int()); 1.688 + break; 1.689 + 1.690 + case T_BOOLEAN: // 1 byte 1.691 + assert(value->type() == T_INT, "Agreement."); 1.692 + _obj->bool_field_put(offset, (jboolean)value->get_int()); 1.693 + break; 1.694 + 1.695 + default: 1.696 + ShouldNotReachHere(); 1.697 + } 1.698 + _i++; 1.699 + } 1.700 +}; 1.701 + 1.702 +// restore elements of an eliminated type array 1.703 +void Deoptimization::reassign_type_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, typeArrayOop obj, BasicType type) { 1.704 + StackValue* low; 1.705 + jlong lval; 1.706 + int index = 0; 1.707 + 1.708 + for (int i = 0; i < sv->field_size(); i++) { 1.709 + StackValue* value = StackValue::create_stack_value(fr, reg_map, sv->field_at(i)); 1.710 + switch(type) { 1.711 + case T_BOOLEAN: obj->bool_at_put (index, (jboolean) value->get_int()); break; 1.712 + case T_BYTE: obj->byte_at_put (index, (jbyte) value->get_int()); break; 1.713 + case T_CHAR: obj->char_at_put (index, (jchar) value->get_int()); break; 1.714 + case T_SHORT: obj->short_at_put(index, (jshort) value->get_int()); break; 1.715 + case T_INT: obj->int_at_put (index, (jint) value->get_int()); break; 1.716 + case T_FLOAT: obj->float_at_put(index, (jfloat) value->get_int()); break; 1.717 + case T_LONG: 1.718 + case T_DOUBLE: 1.719 + low = StackValue::create_stack_value(fr, reg_map, sv->field_at(++i)); 1.720 + lval = jlong_from((jint)value->get_int(), (jint)low->get_int()); 1.721 + sv->value()->long_field_put(index, lval); 1.722 + break; 1.723 + default: 1.724 + ShouldNotReachHere(); 1.725 + } 1.726 + index++; 1.727 + } 1.728 +} 1.729 + 1.730 + 1.731 +// restore fields of an eliminated object array 1.732 +void Deoptimization::reassign_object_array_elements(frame* fr, RegisterMap* reg_map, ObjectValue* sv, objArrayOop obj) { 1.733 + for (int i = 0; i < sv->field_size(); i++) { 1.734 + StackValue* value = StackValue::create_stack_value(fr, reg_map, sv->field_at(i)); 1.735 + assert(value->type() == T_OBJECT, "object element expected"); 1.736 + obj->obj_at_put(i, value->get_obj()()); 1.737 + } 1.738 +} 1.739 + 1.740 + 1.741 +// restore fields of all eliminated objects and arrays 1.742 +void Deoptimization::reassign_fields(frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects) { 1.743 + for (int i = 0; i < objects->length(); i++) { 1.744 + ObjectValue* sv = (ObjectValue*) objects->at(i); 1.745 + KlassHandle k(((ConstantOopReadValue*) sv->klass())->value()()); 1.746 + Handle obj = sv->value(); 1.747 + assert(obj.not_null(), "reallocation was missed"); 1.748 + 1.749 + if (k->oop_is_instance()) { 1.750 + instanceKlass* ik = instanceKlass::cast(k()); 1.751 + FieldReassigner reassign(fr, reg_map, sv, obj()); 1.752 + ik->do_nonstatic_fields(&reassign); 1.753 + } else if (k->oop_is_typeArray()) { 1.754 + typeArrayKlass* ak = typeArrayKlass::cast(k()); 1.755 + reassign_type_array_elements(fr, reg_map, sv, (typeArrayOop) obj(), ak->element_type()); 1.756 + } else if (k->oop_is_objArray()) { 1.757 + reassign_object_array_elements(fr, reg_map, sv, (objArrayOop) obj()); 1.758 + } 1.759 + } 1.760 +} 1.761 + 1.762 + 1.763 +// relock objects for which synchronization was eliminated 1.764 +void Deoptimization::relock_objects(frame* fr, RegisterMap* reg_map, GrowableArray<MonitorValue*>* monitors) { 1.765 + for (int i = 0; i < monitors->length(); i++) { 1.766 + MonitorValue* mv = monitors->at(i); 1.767 + StackValue* owner = StackValue::create_stack_value(fr, reg_map, mv->owner()); 1.768 + if (mv->eliminated()) { 1.769 + Handle obj = owner->get_obj(); 1.770 + assert(obj.not_null(), "reallocation was missed"); 1.771 + BasicLock* lock = StackValue::resolve_monitor_lock(fr, mv->basic_lock()); 1.772 + lock->set_displaced_header(obj->mark()); 1.773 + obj->set_mark((markOop) lock); 1.774 + } 1.775 + assert(owner->get_obj()->is_locked(), "object must be locked now"); 1.776 + } 1.777 +} 1.778 + 1.779 + 1.780 +#ifndef PRODUCT 1.781 +// print information about reallocated objects 1.782 +void Deoptimization::print_objects(GrowableArray<ScopeValue*>* objects) { 1.783 + fieldDescriptor fd; 1.784 + 1.785 + for (int i = 0; i < objects->length(); i++) { 1.786 + ObjectValue* sv = (ObjectValue*) objects->at(i); 1.787 + KlassHandle k(((ConstantOopReadValue*) sv->klass())->value()()); 1.788 + Handle obj = sv->value(); 1.789 + 1.790 + tty->print(" object <" INTPTR_FORMAT "> of type ", sv->value()()); 1.791 + k->as_klassOop()->print_value(); 1.792 + tty->print(" allocated (%d bytes)", obj->size() * HeapWordSize); 1.793 + tty->cr(); 1.794 + 1.795 + if (Verbose) { 1.796 + k->oop_print_on(obj(), tty); 1.797 + } 1.798 + } 1.799 +} 1.800 +#endif 1.801 +#endif // COMPILER2 1.802 + 1.803 +vframeArray* Deoptimization::create_vframeArray(JavaThread* thread, frame fr, RegisterMap *reg_map, GrowableArray<compiledVFrame*>* chunk) { 1.804 + 1.805 +#ifndef PRODUCT 1.806 + if (TraceDeoptimization) { 1.807 + ttyLocker ttyl; 1.808 + tty->print("DEOPT PACKING thread " INTPTR_FORMAT " ", thread); 1.809 + fr.print_on(tty); 1.810 + tty->print_cr(" Virtual frames (innermost first):"); 1.811 + for (int index = 0; index < chunk->length(); index++) { 1.812 + compiledVFrame* vf = chunk->at(index); 1.813 + tty->print(" %2d - ", index); 1.814 + vf->print_value(); 1.815 + int bci = chunk->at(index)->raw_bci(); 1.816 + const char* code_name; 1.817 + if (bci == SynchronizationEntryBCI) { 1.818 + code_name = "sync entry"; 1.819 + } else { 1.820 + Bytecodes::Code code = Bytecodes::code_at(vf->method(), bci); 1.821 + code_name = Bytecodes::name(code); 1.822 + } 1.823 + tty->print(" - %s", code_name); 1.824 + tty->print_cr(" @ bci %d ", bci); 1.825 + if (Verbose) { 1.826 + vf->print(); 1.827 + tty->cr(); 1.828 + } 1.829 + } 1.830 + } 1.831 +#endif 1.832 + 1.833 + // Register map for next frame (used for stack crawl). We capture 1.834 + // the state of the deopt'ing frame's caller. Thus if we need to 1.835 + // stuff a C2I adapter we can properly fill in the callee-save 1.836 + // register locations. 1.837 + frame caller = fr.sender(reg_map); 1.838 + int frame_size = caller.sp() - fr.sp(); 1.839 + 1.840 + frame sender = caller; 1.841 + 1.842 + // Since the Java thread being deoptimized will eventually adjust it's own stack, 1.843 + // the vframeArray containing the unpacking information is allocated in the C heap. 1.844 + // For Compiler1, the caller of the deoptimized frame is saved for use by unpack_frames(). 1.845 + vframeArray* array = vframeArray::allocate(thread, frame_size, chunk, reg_map, sender, caller, fr); 1.846 + 1.847 + // Compare the vframeArray to the collected vframes 1.848 + assert(array->structural_compare(thread, chunk), "just checking"); 1.849 + Events::log("# vframes = %d", (intptr_t)chunk->length()); 1.850 + 1.851 +#ifndef PRODUCT 1.852 + if (TraceDeoptimization) { 1.853 + ttyLocker ttyl; 1.854 + tty->print_cr(" Created vframeArray " INTPTR_FORMAT, array); 1.855 + if (Verbose) { 1.856 + int count = 0; 1.857 + // this used to leak deoptimizedVFrame like it was going out of style!!! 1.858 + for (int index = 0; index < array->frames(); index++ ) { 1.859 + vframeArrayElement* e = array->element(index); 1.860 + e->print(tty); 1.861 + 1.862 + /* 1.863 + No printing yet. 1.864 + array->vframe_at(index)->print_activation(count++); 1.865 + // better as... 1.866 + array->print_activation_for(index, count++); 1.867 + */ 1.868 + } 1.869 + } 1.870 + } 1.871 +#endif // PRODUCT 1.872 + 1.873 + return array; 1.874 +} 1.875 + 1.876 + 1.877 +static void collect_monitors(compiledVFrame* cvf, GrowableArray<Handle>* objects_to_revoke) { 1.878 + GrowableArray<MonitorInfo*>* monitors = cvf->monitors(); 1.879 + for (int i = 0; i < monitors->length(); i++) { 1.880 + MonitorInfo* mon_info = monitors->at(i); 1.881 + if (mon_info->owner() != NULL) { 1.882 + objects_to_revoke->append(Handle(mon_info->owner())); 1.883 + } 1.884 + } 1.885 +} 1.886 + 1.887 + 1.888 +void Deoptimization::revoke_biases_of_monitors(JavaThread* thread, frame fr, RegisterMap* map) { 1.889 + if (!UseBiasedLocking) { 1.890 + return; 1.891 + } 1.892 + 1.893 + GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>(); 1.894 + 1.895 + // Unfortunately we don't have a RegisterMap available in most of 1.896 + // the places we want to call this routine so we need to walk the 1.897 + // stack again to update the register map. 1.898 + if (map == NULL || !map->update_map()) { 1.899 + StackFrameStream sfs(thread, true); 1.900 + bool found = false; 1.901 + while (!found && !sfs.is_done()) { 1.902 + frame* cur = sfs.current(); 1.903 + sfs.next(); 1.904 + found = cur->id() == fr.id(); 1.905 + } 1.906 + assert(found, "frame to be deoptimized not found on target thread's stack"); 1.907 + map = sfs.register_map(); 1.908 + } 1.909 + 1.910 + vframe* vf = vframe::new_vframe(&fr, map, thread); 1.911 + compiledVFrame* cvf = compiledVFrame::cast(vf); 1.912 + // Revoke monitors' biases in all scopes 1.913 + while (!cvf->is_top()) { 1.914 + collect_monitors(cvf, objects_to_revoke); 1.915 + cvf = compiledVFrame::cast(cvf->sender()); 1.916 + } 1.917 + collect_monitors(cvf, objects_to_revoke); 1.918 + 1.919 + if (SafepointSynchronize::is_at_safepoint()) { 1.920 + BiasedLocking::revoke_at_safepoint(objects_to_revoke); 1.921 + } else { 1.922 + BiasedLocking::revoke(objects_to_revoke); 1.923 + } 1.924 +} 1.925 + 1.926 + 1.927 +void Deoptimization::revoke_biases_of_monitors(CodeBlob* cb) { 1.928 + if (!UseBiasedLocking) { 1.929 + return; 1.930 + } 1.931 + 1.932 + assert(SafepointSynchronize::is_at_safepoint(), "must only be called from safepoint"); 1.933 + GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>(); 1.934 + for (JavaThread* jt = Threads::first(); jt != NULL ; jt = jt->next()) { 1.935 + if (jt->has_last_Java_frame()) { 1.936 + StackFrameStream sfs(jt, true); 1.937 + while (!sfs.is_done()) { 1.938 + frame* cur = sfs.current(); 1.939 + if (cb->contains(cur->pc())) { 1.940 + vframe* vf = vframe::new_vframe(cur, sfs.register_map(), jt); 1.941 + compiledVFrame* cvf = compiledVFrame::cast(vf); 1.942 + // Revoke monitors' biases in all scopes 1.943 + while (!cvf->is_top()) { 1.944 + collect_monitors(cvf, objects_to_revoke); 1.945 + cvf = compiledVFrame::cast(cvf->sender()); 1.946 + } 1.947 + collect_monitors(cvf, objects_to_revoke); 1.948 + } 1.949 + sfs.next(); 1.950 + } 1.951 + } 1.952 + } 1.953 + BiasedLocking::revoke_at_safepoint(objects_to_revoke); 1.954 +} 1.955 + 1.956 + 1.957 +void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr) { 1.958 + assert(fr.can_be_deoptimized(), "checking frame type"); 1.959 + 1.960 + gather_statistics(Reason_constraint, Action_none, Bytecodes::_illegal); 1.961 + 1.962 + EventMark m("Deoptimization (pc=" INTPTR_FORMAT ", sp=" INTPTR_FORMAT ")", fr.pc(), fr.id()); 1.963 + 1.964 + // Patch the nmethod so that when execution returns to it we will 1.965 + // deopt the execution state and return to the interpreter. 1.966 + fr.deoptimize(thread); 1.967 +} 1.968 + 1.969 +void Deoptimization::deoptimize(JavaThread* thread, frame fr, RegisterMap *map) { 1.970 + // Deoptimize only if the frame comes from compile code. 1.971 + // Do not deoptimize the frame which is already patched 1.972 + // during the execution of the loops below. 1.973 + if (!fr.is_compiled_frame() || fr.is_deoptimized_frame()) { 1.974 + return; 1.975 + } 1.976 + ResourceMark rm; 1.977 + DeoptimizationMarker dm; 1.978 + if (UseBiasedLocking) { 1.979 + revoke_biases_of_monitors(thread, fr, map); 1.980 + } 1.981 + deoptimize_single_frame(thread, fr); 1.982 + 1.983 +} 1.984 + 1.985 + 1.986 +void Deoptimization::deoptimize_frame(JavaThread* thread, intptr_t* id) { 1.987 + // Compute frame and register map based on thread and sp. 1.988 + RegisterMap reg_map(thread, UseBiasedLocking); 1.989 + frame fr = thread->last_frame(); 1.990 + while (fr.id() != id) { 1.991 + fr = fr.sender(®_map); 1.992 + } 1.993 + deoptimize(thread, fr, ®_map); 1.994 +} 1.995 + 1.996 + 1.997 +// JVMTI PopFrame support 1.998 +JRT_LEAF(void, Deoptimization::popframe_preserve_args(JavaThread* thread, int bytes_to_save, void* start_address)) 1.999 +{ 1.1000 + thread->popframe_preserve_args(in_ByteSize(bytes_to_save), start_address); 1.1001 +} 1.1002 +JRT_END 1.1003 + 1.1004 + 1.1005 +#ifdef COMPILER2 1.1006 +void Deoptimization::load_class_by_index(constantPoolHandle constant_pool, int index, TRAPS) { 1.1007 + // in case of an unresolved klass entry, load the class. 1.1008 + if (constant_pool->tag_at(index).is_unresolved_klass()) { 1.1009 + klassOop tk = constant_pool->klass_at(index, CHECK); 1.1010 + return; 1.1011 + } 1.1012 + 1.1013 + if (!constant_pool->tag_at(index).is_symbol()) return; 1.1014 + 1.1015 + Handle class_loader (THREAD, instanceKlass::cast(constant_pool->pool_holder())->class_loader()); 1.1016 + symbolHandle symbol (THREAD, constant_pool->symbol_at(index)); 1.1017 + 1.1018 + // class name? 1.1019 + if (symbol->byte_at(0) != '(') { 1.1020 + Handle protection_domain (THREAD, Klass::cast(constant_pool->pool_holder())->protection_domain()); 1.1021 + SystemDictionary::resolve_or_null(symbol, class_loader, protection_domain, CHECK); 1.1022 + return; 1.1023 + } 1.1024 + 1.1025 + // then it must be a signature! 1.1026 + for (SignatureStream ss(symbol); !ss.is_done(); ss.next()) { 1.1027 + if (ss.is_object()) { 1.1028 + symbolOop s = ss.as_symbol(CHECK); 1.1029 + symbolHandle class_name (THREAD, s); 1.1030 + Handle protection_domain (THREAD, Klass::cast(constant_pool->pool_holder())->protection_domain()); 1.1031 + SystemDictionary::resolve_or_null(class_name, class_loader, protection_domain, CHECK); 1.1032 + } 1.1033 + } 1.1034 +} 1.1035 + 1.1036 + 1.1037 +void Deoptimization::load_class_by_index(constantPoolHandle constant_pool, int index) { 1.1038 + EXCEPTION_MARK; 1.1039 + load_class_by_index(constant_pool, index, THREAD); 1.1040 + if (HAS_PENDING_EXCEPTION) { 1.1041 + // Exception happened during classloading. We ignore the exception here, since it 1.1042 + // is going to be rethrown since the current activation is going to be deoptimzied and 1.1043 + // the interpreter will re-execute the bytecode. 1.1044 + CLEAR_PENDING_EXCEPTION; 1.1045 + } 1.1046 +} 1.1047 + 1.1048 +JRT_ENTRY(void, Deoptimization::uncommon_trap_inner(JavaThread* thread, jint trap_request)) { 1.1049 + HandleMark hm; 1.1050 + 1.1051 + // uncommon_trap() is called at the beginning of the uncommon trap 1.1052 + // handler. Note this fact before we start generating temporary frames 1.1053 + // that can confuse an asynchronous stack walker. This counter is 1.1054 + // decremented at the end of unpack_frames(). 1.1055 + thread->inc_in_deopt_handler(); 1.1056 + 1.1057 + // We need to update the map if we have biased locking. 1.1058 + RegisterMap reg_map(thread, UseBiasedLocking); 1.1059 + frame stub_frame = thread->last_frame(); 1.1060 + frame fr = stub_frame.sender(®_map); 1.1061 + // Make sure the calling nmethod is not getting deoptimized and removed 1.1062 + // before we are done with it. 1.1063 + nmethodLocker nl(fr.pc()); 1.1064 + 1.1065 + { 1.1066 + ResourceMark rm; 1.1067 + 1.1068 + // Revoke biases of any monitors in the frame to ensure we can migrate them 1.1069 + revoke_biases_of_monitors(thread, fr, ®_map); 1.1070 + 1.1071 + DeoptReason reason = trap_request_reason(trap_request); 1.1072 + DeoptAction action = trap_request_action(trap_request); 1.1073 + jint unloaded_class_index = trap_request_index(trap_request); // CP idx or -1 1.1074 + 1.1075 + Events::log("Uncommon trap occurred @" INTPTR_FORMAT " unloaded_class_index = %d", fr.pc(), (int) trap_request); 1.1076 + vframe* vf = vframe::new_vframe(&fr, ®_map, thread); 1.1077 + compiledVFrame* cvf = compiledVFrame::cast(vf); 1.1078 + 1.1079 + nmethod* nm = cvf->code(); 1.1080 + 1.1081 + ScopeDesc* trap_scope = cvf->scope(); 1.1082 + methodHandle trap_method = trap_scope->method(); 1.1083 + int trap_bci = trap_scope->bci(); 1.1084 + Bytecodes::Code trap_bc = Bytecode_at(trap_method->bcp_from(trap_bci))->java_code(); 1.1085 + 1.1086 + // Record this event in the histogram. 1.1087 + gather_statistics(reason, action, trap_bc); 1.1088 + 1.1089 + // Ensure that we can record deopt. history: 1.1090 + bool create_if_missing = ProfileTraps; 1.1091 + 1.1092 + methodDataHandle trap_mdo 1.1093 + (THREAD, get_method_data(thread, trap_method, create_if_missing)); 1.1094 + 1.1095 + // Print a bunch of diagnostics, if requested. 1.1096 + if (TraceDeoptimization || LogCompilation) { 1.1097 + ResourceMark rm; 1.1098 + ttyLocker ttyl; 1.1099 + char buf[100]; 1.1100 + if (xtty != NULL) { 1.1101 + xtty->begin_head("uncommon_trap thread='" UINTX_FORMAT"' %s", 1.1102 + os::current_thread_id(), 1.1103 + format_trap_request(buf, sizeof(buf), trap_request)); 1.1104 + nm->log_identity(xtty); 1.1105 + } 1.1106 + symbolHandle class_name; 1.1107 + bool unresolved = false; 1.1108 + if (unloaded_class_index >= 0) { 1.1109 + constantPoolHandle constants (THREAD, trap_method->constants()); 1.1110 + if (constants->tag_at(unloaded_class_index).is_unresolved_klass()) { 1.1111 + class_name = symbolHandle(THREAD, 1.1112 + constants->klass_name_at(unloaded_class_index)); 1.1113 + unresolved = true; 1.1114 + if (xtty != NULL) 1.1115 + xtty->print(" unresolved='1'"); 1.1116 + } else if (constants->tag_at(unloaded_class_index).is_symbol()) { 1.1117 + class_name = symbolHandle(THREAD, 1.1118 + constants->symbol_at(unloaded_class_index)); 1.1119 + } 1.1120 + if (xtty != NULL) 1.1121 + xtty->name(class_name); 1.1122 + } 1.1123 + if (xtty != NULL && trap_mdo.not_null()) { 1.1124 + // Dump the relevant MDO state. 1.1125 + // This is the deopt count for the current reason, any previous 1.1126 + // reasons or recompiles seen at this point. 1.1127 + int dcnt = trap_mdo->trap_count(reason); 1.1128 + if (dcnt != 0) 1.1129 + xtty->print(" count='%d'", dcnt); 1.1130 + ProfileData* pdata = trap_mdo->bci_to_data(trap_bci); 1.1131 + int dos = (pdata == NULL)? 0: pdata->trap_state(); 1.1132 + if (dos != 0) { 1.1133 + xtty->print(" state='%s'", format_trap_state(buf, sizeof(buf), dos)); 1.1134 + if (trap_state_is_recompiled(dos)) { 1.1135 + int recnt2 = trap_mdo->overflow_recompile_count(); 1.1136 + if (recnt2 != 0) 1.1137 + xtty->print(" recompiles2='%d'", recnt2); 1.1138 + } 1.1139 + } 1.1140 + } 1.1141 + if (xtty != NULL) { 1.1142 + xtty->stamp(); 1.1143 + xtty->end_head(); 1.1144 + } 1.1145 + if (TraceDeoptimization) { // make noise on the tty 1.1146 + tty->print("Uncommon trap occurred in"); 1.1147 + nm->method()->print_short_name(tty); 1.1148 + tty->print(" (@" INTPTR_FORMAT ") thread=%d reason=%s action=%s unloaded_class_index=%d", 1.1149 + fr.pc(), 1.1150 + (int) os::current_thread_id(), 1.1151 + trap_reason_name(reason), 1.1152 + trap_action_name(action), 1.1153 + unloaded_class_index); 1.1154 + if (class_name.not_null()) { 1.1155 + tty->print(unresolved ? " unresolved class: " : " symbol: "); 1.1156 + class_name->print_symbol_on(tty); 1.1157 + } 1.1158 + tty->cr(); 1.1159 + } 1.1160 + if (xtty != NULL) { 1.1161 + // Log the precise location of the trap. 1.1162 + for (ScopeDesc* sd = trap_scope; ; sd = sd->sender()) { 1.1163 + xtty->begin_elem("jvms bci='%d'", sd->bci()); 1.1164 + xtty->method(sd->method()); 1.1165 + xtty->end_elem(); 1.1166 + if (sd->is_top()) break; 1.1167 + } 1.1168 + xtty->tail("uncommon_trap"); 1.1169 + } 1.1170 + } 1.1171 + // (End diagnostic printout.) 1.1172 + 1.1173 + // Load class if necessary 1.1174 + if (unloaded_class_index >= 0) { 1.1175 + constantPoolHandle constants(THREAD, trap_method->constants()); 1.1176 + load_class_by_index(constants, unloaded_class_index); 1.1177 + } 1.1178 + 1.1179 + // Flush the nmethod if necessary and desirable. 1.1180 + // 1.1181 + // We need to avoid situations where we are re-flushing the nmethod 1.1182 + // because of a hot deoptimization site. Repeated flushes at the same 1.1183 + // point need to be detected by the compiler and avoided. If the compiler 1.1184 + // cannot avoid them (or has a bug and "refuses" to avoid them), this 1.1185 + // module must take measures to avoid an infinite cycle of recompilation 1.1186 + // and deoptimization. There are several such measures: 1.1187 + // 1.1188 + // 1. If a recompilation is ordered a second time at some site X 1.1189 + // and for the same reason R, the action is adjusted to 'reinterpret', 1.1190 + // to give the interpreter time to exercise the method more thoroughly. 1.1191 + // If this happens, the method's overflow_recompile_count is incremented. 1.1192 + // 1.1193 + // 2. If the compiler fails to reduce the deoptimization rate, then 1.1194 + // the method's overflow_recompile_count will begin to exceed the set 1.1195 + // limit PerBytecodeRecompilationCutoff. If this happens, the action 1.1196 + // is adjusted to 'make_not_compilable', and the method is abandoned 1.1197 + // to the interpreter. This is a performance hit for hot methods, 1.1198 + // but is better than a disastrous infinite cycle of recompilations. 1.1199 + // (Actually, only the method containing the site X is abandoned.) 1.1200 + // 1.1201 + // 3. In parallel with the previous measures, if the total number of 1.1202 + // recompilations of a method exceeds the much larger set limit 1.1203 + // PerMethodRecompilationCutoff, the method is abandoned. 1.1204 + // This should only happen if the method is very large and has 1.1205 + // many "lukewarm" deoptimizations. The code which enforces this 1.1206 + // limit is elsewhere (class nmethod, class methodOopDesc). 1.1207 + // 1.1208 + // Note that the per-BCI 'is_recompiled' bit gives the compiler one chance 1.1209 + // to recompile at each bytecode independently of the per-BCI cutoff. 1.1210 + // 1.1211 + // The decision to update code is up to the compiler, and is encoded 1.1212 + // in the Action_xxx code. If the compiler requests Action_none 1.1213 + // no trap state is changed, no compiled code is changed, and the 1.1214 + // computation suffers along in the interpreter. 1.1215 + // 1.1216 + // The other action codes specify various tactics for decompilation 1.1217 + // and recompilation. Action_maybe_recompile is the loosest, and 1.1218 + // allows the compiled code to stay around until enough traps are seen, 1.1219 + // and until the compiler gets around to recompiling the trapping method. 1.1220 + // 1.1221 + // The other actions cause immediate removal of the present code. 1.1222 + 1.1223 + bool update_trap_state = true; 1.1224 + bool make_not_entrant = false; 1.1225 + bool make_not_compilable = false; 1.1226 + bool reset_counters = false; 1.1227 + switch (action) { 1.1228 + case Action_none: 1.1229 + // Keep the old code. 1.1230 + update_trap_state = false; 1.1231 + break; 1.1232 + case Action_maybe_recompile: 1.1233 + // Do not need to invalidate the present code, but we can 1.1234 + // initiate another 1.1235 + // Start compiler without (necessarily) invalidating the nmethod. 1.1236 + // The system will tolerate the old code, but new code should be 1.1237 + // generated when possible. 1.1238 + break; 1.1239 + case Action_reinterpret: 1.1240 + // Go back into the interpreter for a while, and then consider 1.1241 + // recompiling form scratch. 1.1242 + make_not_entrant = true; 1.1243 + // Reset invocation counter for outer most method. 1.1244 + // This will allow the interpreter to exercise the bytecodes 1.1245 + // for a while before recompiling. 1.1246 + // By contrast, Action_make_not_entrant is immediate. 1.1247 + // 1.1248 + // Note that the compiler will track null_check, null_assert, 1.1249 + // range_check, and class_check events and log them as if they 1.1250 + // had been traps taken from compiled code. This will update 1.1251 + // the MDO trap history so that the next compilation will 1.1252 + // properly detect hot trap sites. 1.1253 + reset_counters = true; 1.1254 + break; 1.1255 + case Action_make_not_entrant: 1.1256 + // Request immediate recompilation, and get rid of the old code. 1.1257 + // Make them not entrant, so next time they are called they get 1.1258 + // recompiled. Unloaded classes are loaded now so recompile before next 1.1259 + // time they are called. Same for uninitialized. The interpreter will 1.1260 + // link the missing class, if any. 1.1261 + make_not_entrant = true; 1.1262 + break; 1.1263 + case Action_make_not_compilable: 1.1264 + // Give up on compiling this method at all. 1.1265 + make_not_entrant = true; 1.1266 + make_not_compilable = true; 1.1267 + break; 1.1268 + default: 1.1269 + ShouldNotReachHere(); 1.1270 + } 1.1271 + 1.1272 + // Setting +ProfileTraps fixes the following, on all platforms: 1.1273 + // 4852688: ProfileInterpreter is off by default for ia64. The result is 1.1274 + // infinite heroic-opt-uncommon-trap/deopt/recompile cycles, since the 1.1275 + // recompile relies on a methodDataOop to record heroic opt failures. 1.1276 + 1.1277 + // Whether the interpreter is producing MDO data or not, we also need 1.1278 + // to use the MDO to detect hot deoptimization points and control 1.1279 + // aggressive optimization. 1.1280 + if (ProfileTraps && update_trap_state && trap_mdo.not_null()) { 1.1281 + assert(trap_mdo() == get_method_data(thread, trap_method, false), "sanity"); 1.1282 + uint this_trap_count = 0; 1.1283 + bool maybe_prior_trap = false; 1.1284 + bool maybe_prior_recompile = false; 1.1285 + ProfileData* pdata 1.1286 + = query_update_method_data(trap_mdo, trap_bci, reason, 1.1287 + //outputs: 1.1288 + this_trap_count, 1.1289 + maybe_prior_trap, 1.1290 + maybe_prior_recompile); 1.1291 + // Because the interpreter also counts null, div0, range, and class 1.1292 + // checks, these traps from compiled code are double-counted. 1.1293 + // This is harmless; it just means that the PerXTrapLimit values 1.1294 + // are in effect a little smaller than they look. 1.1295 + 1.1296 + DeoptReason per_bc_reason = reason_recorded_per_bytecode_if_any(reason); 1.1297 + if (per_bc_reason != Reason_none) { 1.1298 + // Now take action based on the partially known per-BCI history. 1.1299 + if (maybe_prior_trap 1.1300 + && this_trap_count >= (uint)PerBytecodeTrapLimit) { 1.1301 + // If there are too many traps at this BCI, force a recompile. 1.1302 + // This will allow the compiler to see the limit overflow, and 1.1303 + // take corrective action, if possible. The compiler generally 1.1304 + // does not use the exact PerBytecodeTrapLimit value, but instead 1.1305 + // changes its tactics if it sees any traps at all. This provides 1.1306 + // a little hysteresis, delaying a recompile until a trap happens 1.1307 + // several times. 1.1308 + // 1.1309 + // Actually, since there is only one bit of counter per BCI, 1.1310 + // the possible per-BCI counts are {0,1,(per-method count)}. 1.1311 + // This produces accurate results if in fact there is only 1.1312 + // one hot trap site, but begins to get fuzzy if there are 1.1313 + // many sites. For example, if there are ten sites each 1.1314 + // trapping two or more times, they each get the blame for 1.1315 + // all of their traps. 1.1316 + make_not_entrant = true; 1.1317 + } 1.1318 + 1.1319 + // Detect repeated recompilation at the same BCI, and enforce a limit. 1.1320 + if (make_not_entrant && maybe_prior_recompile) { 1.1321 + // More than one recompile at this point. 1.1322 + trap_mdo->inc_overflow_recompile_count(); 1.1323 + if (maybe_prior_trap 1.1324 + && ((uint)trap_mdo->overflow_recompile_count() 1.1325 + > (uint)PerBytecodeRecompilationCutoff)) { 1.1326 + // Give up on the method containing the bad BCI. 1.1327 + if (trap_method() == nm->method()) { 1.1328 + make_not_compilable = true; 1.1329 + } else { 1.1330 + trap_method->set_not_compilable(); 1.1331 + // But give grace to the enclosing nm->method(). 1.1332 + } 1.1333 + } 1.1334 + } 1.1335 + } else { 1.1336 + // For reasons which are not recorded per-bytecode, we simply 1.1337 + // force recompiles unconditionally. 1.1338 + // (Note that PerMethodRecompilationCutoff is enforced elsewhere.) 1.1339 + make_not_entrant = true; 1.1340 + } 1.1341 + 1.1342 + // Go back to the compiler if there are too many traps in this method. 1.1343 + if (this_trap_count >= (uint)PerMethodTrapLimit) { 1.1344 + // If there are too many traps in this method, force a recompile. 1.1345 + // This will allow the compiler to see the limit overflow, and 1.1346 + // take corrective action, if possible. 1.1347 + // (This condition is an unlikely backstop only, because the 1.1348 + // PerBytecodeTrapLimit is more likely to take effect first, 1.1349 + // if it is applicable.) 1.1350 + make_not_entrant = true; 1.1351 + } 1.1352 + 1.1353 + // Here's more hysteresis: If there has been a recompile at 1.1354 + // this trap point already, run the method in the interpreter 1.1355 + // for a while to exercise it more thoroughly. 1.1356 + if (make_not_entrant && maybe_prior_recompile && maybe_prior_trap) { 1.1357 + reset_counters = true; 1.1358 + } 1.1359 + 1.1360 + if (make_not_entrant && pdata != NULL) { 1.1361 + // Record the recompilation event, if any. 1.1362 + int tstate0 = pdata->trap_state(); 1.1363 + int tstate1 = trap_state_set_recompiled(tstate0, true); 1.1364 + if (tstate1 != tstate0) 1.1365 + pdata->set_trap_state(tstate1); 1.1366 + } 1.1367 + } 1.1368 + 1.1369 + // Take requested actions on the method: 1.1370 + 1.1371 + // Reset invocation counters 1.1372 + if (reset_counters) { 1.1373 + if (nm->is_osr_method()) 1.1374 + reset_invocation_counter(trap_scope, CompileThreshold); 1.1375 + else 1.1376 + reset_invocation_counter(trap_scope); 1.1377 + } 1.1378 + 1.1379 + // Recompile 1.1380 + if (make_not_entrant) { 1.1381 + nm->make_not_entrant(); 1.1382 + } 1.1383 + 1.1384 + // Give up compiling 1.1385 + if (make_not_compilable) { 1.1386 + assert(make_not_entrant, "consistent"); 1.1387 + nm->method()->set_not_compilable(); 1.1388 + } 1.1389 + 1.1390 + } // Free marked resources 1.1391 + 1.1392 +} 1.1393 +JRT_END 1.1394 + 1.1395 +methodDataOop 1.1396 +Deoptimization::get_method_data(JavaThread* thread, methodHandle m, 1.1397 + bool create_if_missing) { 1.1398 + Thread* THREAD = thread; 1.1399 + methodDataOop mdo = m()->method_data(); 1.1400 + if (mdo == NULL && create_if_missing && !HAS_PENDING_EXCEPTION) { 1.1401 + // Build an MDO. Ignore errors like OutOfMemory; 1.1402 + // that simply means we won't have an MDO to update. 1.1403 + methodOopDesc::build_interpreter_method_data(m, THREAD); 1.1404 + if (HAS_PENDING_EXCEPTION) { 1.1405 + assert((PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())), "we expect only an OOM error here"); 1.1406 + CLEAR_PENDING_EXCEPTION; 1.1407 + } 1.1408 + mdo = m()->method_data(); 1.1409 + } 1.1410 + return mdo; 1.1411 +} 1.1412 + 1.1413 +ProfileData* 1.1414 +Deoptimization::query_update_method_data(methodDataHandle trap_mdo, 1.1415 + int trap_bci, 1.1416 + Deoptimization::DeoptReason reason, 1.1417 + //outputs: 1.1418 + uint& ret_this_trap_count, 1.1419 + bool& ret_maybe_prior_trap, 1.1420 + bool& ret_maybe_prior_recompile) { 1.1421 + uint prior_trap_count = trap_mdo->trap_count(reason); 1.1422 + uint this_trap_count = trap_mdo->inc_trap_count(reason); 1.1423 + 1.1424 + // If the runtime cannot find a place to store trap history, 1.1425 + // it is estimated based on the general condition of the method. 1.1426 + // If the method has ever been recompiled, or has ever incurred 1.1427 + // a trap with the present reason , then this BCI is assumed 1.1428 + // (pessimistically) to be the culprit. 1.1429 + bool maybe_prior_trap = (prior_trap_count != 0); 1.1430 + bool maybe_prior_recompile = (trap_mdo->decompile_count() != 0); 1.1431 + ProfileData* pdata = NULL; 1.1432 + 1.1433 + 1.1434 + // For reasons which are recorded per bytecode, we check per-BCI data. 1.1435 + DeoptReason per_bc_reason = reason_recorded_per_bytecode_if_any(reason); 1.1436 + if (per_bc_reason != Reason_none) { 1.1437 + // Find the profile data for this BCI. If there isn't one, 1.1438 + // try to allocate one from the MDO's set of spares. 1.1439 + // This will let us detect a repeated trap at this point. 1.1440 + pdata = trap_mdo->allocate_bci_to_data(trap_bci); 1.1441 + 1.1442 + if (pdata != NULL) { 1.1443 + // Query the trap state of this profile datum. 1.1444 + int tstate0 = pdata->trap_state(); 1.1445 + if (!trap_state_has_reason(tstate0, per_bc_reason)) 1.1446 + maybe_prior_trap = false; 1.1447 + if (!trap_state_is_recompiled(tstate0)) 1.1448 + maybe_prior_recompile = false; 1.1449 + 1.1450 + // Update the trap state of this profile datum. 1.1451 + int tstate1 = tstate0; 1.1452 + // Record the reason. 1.1453 + tstate1 = trap_state_add_reason(tstate1, per_bc_reason); 1.1454 + // Store the updated state on the MDO, for next time. 1.1455 + if (tstate1 != tstate0) 1.1456 + pdata->set_trap_state(tstate1); 1.1457 + } else { 1.1458 + if (LogCompilation && xtty != NULL) 1.1459 + // Missing MDP? Leave a small complaint in the log. 1.1460 + xtty->elem("missing_mdp bci='%d'", trap_bci); 1.1461 + } 1.1462 + } 1.1463 + 1.1464 + // Return results: 1.1465 + ret_this_trap_count = this_trap_count; 1.1466 + ret_maybe_prior_trap = maybe_prior_trap; 1.1467 + ret_maybe_prior_recompile = maybe_prior_recompile; 1.1468 + return pdata; 1.1469 +} 1.1470 + 1.1471 +void 1.1472 +Deoptimization::update_method_data_from_interpreter(methodDataHandle trap_mdo, int trap_bci, int reason) { 1.1473 + ResourceMark rm; 1.1474 + // Ignored outputs: 1.1475 + uint ignore_this_trap_count; 1.1476 + bool ignore_maybe_prior_trap; 1.1477 + bool ignore_maybe_prior_recompile; 1.1478 + query_update_method_data(trap_mdo, trap_bci, 1.1479 + (DeoptReason)reason, 1.1480 + ignore_this_trap_count, 1.1481 + ignore_maybe_prior_trap, 1.1482 + ignore_maybe_prior_recompile); 1.1483 +} 1.1484 + 1.1485 +void Deoptimization::reset_invocation_counter(ScopeDesc* trap_scope, jint top_count) { 1.1486 + ScopeDesc* sd = trap_scope; 1.1487 + for (; !sd->is_top(); sd = sd->sender()) { 1.1488 + // Reset ICs of inlined methods, since they can trigger compilations also. 1.1489 + sd->method()->invocation_counter()->reset(); 1.1490 + } 1.1491 + InvocationCounter* c = sd->method()->invocation_counter(); 1.1492 + if (top_count != _no_count) { 1.1493 + // It was an OSR method, so bump the count higher. 1.1494 + c->set(c->state(), top_count); 1.1495 + } else { 1.1496 + c->reset(); 1.1497 + } 1.1498 + sd->method()->backedge_counter()->reset(); 1.1499 +} 1.1500 + 1.1501 +Deoptimization::UnrollBlock* Deoptimization::uncommon_trap(JavaThread* thread, jint trap_request) { 1.1502 + 1.1503 + // Still in Java no safepoints 1.1504 + { 1.1505 + // This enters VM and may safepoint 1.1506 + uncommon_trap_inner(thread, trap_request); 1.1507 + } 1.1508 + return fetch_unroll_info_helper(thread); 1.1509 +} 1.1510 + 1.1511 +// Local derived constants. 1.1512 +// Further breakdown of DataLayout::trap_state, as promised by DataLayout. 1.1513 +const int DS_REASON_MASK = DataLayout::trap_mask >> 1; 1.1514 +const int DS_RECOMPILE_BIT = DataLayout::trap_mask - DS_REASON_MASK; 1.1515 + 1.1516 +//---------------------------trap_state_reason--------------------------------- 1.1517 +Deoptimization::DeoptReason 1.1518 +Deoptimization::trap_state_reason(int trap_state) { 1.1519 + // This assert provides the link between the width of DataLayout::trap_bits 1.1520 + // and the encoding of "recorded" reasons. It ensures there are enough 1.1521 + // bits to store all needed reasons in the per-BCI MDO profile. 1.1522 + assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits"); 1.1523 + int recompile_bit = (trap_state & DS_RECOMPILE_BIT); 1.1524 + trap_state -= recompile_bit; 1.1525 + if (trap_state == DS_REASON_MASK) { 1.1526 + return Reason_many; 1.1527 + } else { 1.1528 + assert((int)Reason_none == 0, "state=0 => Reason_none"); 1.1529 + return (DeoptReason)trap_state; 1.1530 + } 1.1531 +} 1.1532 +//-------------------------trap_state_has_reason------------------------------- 1.1533 +int Deoptimization::trap_state_has_reason(int trap_state, int reason) { 1.1534 + assert(reason_is_recorded_per_bytecode((DeoptReason)reason), "valid reason"); 1.1535 + assert(DS_REASON_MASK >= Reason_RECORDED_LIMIT, "enough bits"); 1.1536 + int recompile_bit = (trap_state & DS_RECOMPILE_BIT); 1.1537 + trap_state -= recompile_bit; 1.1538 + if (trap_state == DS_REASON_MASK) { 1.1539 + return -1; // true, unspecifically (bottom of state lattice) 1.1540 + } else if (trap_state == reason) { 1.1541 + return 1; // true, definitely 1.1542 + } else if (trap_state == 0) { 1.1543 + return 0; // false, definitely (top of state lattice) 1.1544 + } else { 1.1545 + return 0; // false, definitely 1.1546 + } 1.1547 +} 1.1548 +//-------------------------trap_state_add_reason------------------------------- 1.1549 +int Deoptimization::trap_state_add_reason(int trap_state, int reason) { 1.1550 + assert(reason_is_recorded_per_bytecode((DeoptReason)reason) || reason == Reason_many, "valid reason"); 1.1551 + int recompile_bit = (trap_state & DS_RECOMPILE_BIT); 1.1552 + trap_state -= recompile_bit; 1.1553 + if (trap_state == DS_REASON_MASK) { 1.1554 + return trap_state + recompile_bit; // already at state lattice bottom 1.1555 + } else if (trap_state == reason) { 1.1556 + return trap_state + recompile_bit; // the condition is already true 1.1557 + } else if (trap_state == 0) { 1.1558 + return reason + recompile_bit; // no condition has yet been true 1.1559 + } else { 1.1560 + return DS_REASON_MASK + recompile_bit; // fall to state lattice bottom 1.1561 + } 1.1562 +} 1.1563 +//-----------------------trap_state_is_recompiled------------------------------ 1.1564 +bool Deoptimization::trap_state_is_recompiled(int trap_state) { 1.1565 + return (trap_state & DS_RECOMPILE_BIT) != 0; 1.1566 +} 1.1567 +//-----------------------trap_state_set_recompiled----------------------------- 1.1568 +int Deoptimization::trap_state_set_recompiled(int trap_state, bool z) { 1.1569 + if (z) return trap_state | DS_RECOMPILE_BIT; 1.1570 + else return trap_state & ~DS_RECOMPILE_BIT; 1.1571 +} 1.1572 +//---------------------------format_trap_state--------------------------------- 1.1573 +// This is used for debugging and diagnostics, including hotspot.log output. 1.1574 +const char* Deoptimization::format_trap_state(char* buf, size_t buflen, 1.1575 + int trap_state) { 1.1576 + DeoptReason reason = trap_state_reason(trap_state); 1.1577 + bool recomp_flag = trap_state_is_recompiled(trap_state); 1.1578 + // Re-encode the state from its decoded components. 1.1579 + int decoded_state = 0; 1.1580 + if (reason_is_recorded_per_bytecode(reason) || reason == Reason_many) 1.1581 + decoded_state = trap_state_add_reason(decoded_state, reason); 1.1582 + if (recomp_flag) 1.1583 + decoded_state = trap_state_set_recompiled(decoded_state, recomp_flag); 1.1584 + // If the state re-encodes properly, format it symbolically. 1.1585 + // Because this routine is used for debugging and diagnostics, 1.1586 + // be robust even if the state is a strange value. 1.1587 + size_t len; 1.1588 + if (decoded_state != trap_state) { 1.1589 + // Random buggy state that doesn't decode?? 1.1590 + len = jio_snprintf(buf, buflen, "#%d", trap_state); 1.1591 + } else { 1.1592 + len = jio_snprintf(buf, buflen, "%s%s", 1.1593 + trap_reason_name(reason), 1.1594 + recomp_flag ? " recompiled" : ""); 1.1595 + } 1.1596 + if (len >= buflen) 1.1597 + buf[buflen-1] = '\0'; 1.1598 + return buf; 1.1599 +} 1.1600 + 1.1601 + 1.1602 +//--------------------------------statics-------------------------------------- 1.1603 +Deoptimization::DeoptAction Deoptimization::_unloaded_action 1.1604 + = Deoptimization::Action_reinterpret; 1.1605 +const char* Deoptimization::_trap_reason_name[Reason_LIMIT] = { 1.1606 + // Note: Keep this in sync. with enum DeoptReason. 1.1607 + "none", 1.1608 + "null_check", 1.1609 + "null_assert", 1.1610 + "range_check", 1.1611 + "class_check", 1.1612 + "array_check", 1.1613 + "intrinsic", 1.1614 + "unloaded", 1.1615 + "uninitialized", 1.1616 + "unreached", 1.1617 + "unhandled", 1.1618 + "constraint", 1.1619 + "div0_check", 1.1620 + "age" 1.1621 +}; 1.1622 +const char* Deoptimization::_trap_action_name[Action_LIMIT] = { 1.1623 + // Note: Keep this in sync. with enum DeoptAction. 1.1624 + "none", 1.1625 + "maybe_recompile", 1.1626 + "reinterpret", 1.1627 + "make_not_entrant", 1.1628 + "make_not_compilable" 1.1629 +}; 1.1630 + 1.1631 +const char* Deoptimization::trap_reason_name(int reason) { 1.1632 + if (reason == Reason_many) return "many"; 1.1633 + if ((uint)reason < Reason_LIMIT) 1.1634 + return _trap_reason_name[reason]; 1.1635 + static char buf[20]; 1.1636 + sprintf(buf, "reason%d", reason); 1.1637 + return buf; 1.1638 +} 1.1639 +const char* Deoptimization::trap_action_name(int action) { 1.1640 + if ((uint)action < Action_LIMIT) 1.1641 + return _trap_action_name[action]; 1.1642 + static char buf[20]; 1.1643 + sprintf(buf, "action%d", action); 1.1644 + return buf; 1.1645 +} 1.1646 + 1.1647 +// This is used for debugging and diagnostics, including hotspot.log output. 1.1648 +const char* Deoptimization::format_trap_request(char* buf, size_t buflen, 1.1649 + int trap_request) { 1.1650 + jint unloaded_class_index = trap_request_index(trap_request); 1.1651 + const char* reason = trap_reason_name(trap_request_reason(trap_request)); 1.1652 + const char* action = trap_action_name(trap_request_action(trap_request)); 1.1653 + size_t len; 1.1654 + if (unloaded_class_index < 0) { 1.1655 + len = jio_snprintf(buf, buflen, "reason='%s' action='%s'", 1.1656 + reason, action); 1.1657 + } else { 1.1658 + len = jio_snprintf(buf, buflen, "reason='%s' action='%s' index='%d'", 1.1659 + reason, action, unloaded_class_index); 1.1660 + } 1.1661 + if (len >= buflen) 1.1662 + buf[buflen-1] = '\0'; 1.1663 + return buf; 1.1664 +} 1.1665 + 1.1666 +juint Deoptimization::_deoptimization_hist 1.1667 + [Deoptimization::Reason_LIMIT] 1.1668 + [1 + Deoptimization::Action_LIMIT] 1.1669 + [Deoptimization::BC_CASE_LIMIT] 1.1670 + = {0}; 1.1671 + 1.1672 +enum { 1.1673 + LSB_BITS = 8, 1.1674 + LSB_MASK = right_n_bits(LSB_BITS) 1.1675 +}; 1.1676 + 1.1677 +void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action, 1.1678 + Bytecodes::Code bc) { 1.1679 + assert(reason >= 0 && reason < Reason_LIMIT, "oob"); 1.1680 + assert(action >= 0 && action < Action_LIMIT, "oob"); 1.1681 + _deoptimization_hist[Reason_none][0][0] += 1; // total 1.1682 + _deoptimization_hist[reason][0][0] += 1; // per-reason total 1.1683 + juint* cases = _deoptimization_hist[reason][1+action]; 1.1684 + juint* bc_counter_addr = NULL; 1.1685 + juint bc_counter = 0; 1.1686 + // Look for an unused counter, or an exact match to this BC. 1.1687 + if (bc != Bytecodes::_illegal) { 1.1688 + for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) { 1.1689 + juint* counter_addr = &cases[bc_case]; 1.1690 + juint counter = *counter_addr; 1.1691 + if ((counter == 0 && bc_counter_addr == NULL) 1.1692 + || (Bytecodes::Code)(counter & LSB_MASK) == bc) { 1.1693 + // this counter is either free or is already devoted to this BC 1.1694 + bc_counter_addr = counter_addr; 1.1695 + bc_counter = counter | bc; 1.1696 + } 1.1697 + } 1.1698 + } 1.1699 + if (bc_counter_addr == NULL) { 1.1700 + // Overflow, or no given bytecode. 1.1701 + bc_counter_addr = &cases[BC_CASE_LIMIT-1]; 1.1702 + bc_counter = (*bc_counter_addr & ~LSB_MASK); // clear LSB 1.1703 + } 1.1704 + *bc_counter_addr = bc_counter + (1 << LSB_BITS); 1.1705 +} 1.1706 + 1.1707 +jint Deoptimization::total_deoptimization_count() { 1.1708 + return _deoptimization_hist[Reason_none][0][0]; 1.1709 +} 1.1710 + 1.1711 +jint Deoptimization::deoptimization_count(DeoptReason reason) { 1.1712 + assert(reason >= 0 && reason < Reason_LIMIT, "oob"); 1.1713 + return _deoptimization_hist[reason][0][0]; 1.1714 +} 1.1715 + 1.1716 +void Deoptimization::print_statistics() { 1.1717 + juint total = total_deoptimization_count(); 1.1718 + juint account = total; 1.1719 + if (total != 0) { 1.1720 + ttyLocker ttyl; 1.1721 + if (xtty != NULL) xtty->head("statistics type='deoptimization'"); 1.1722 + tty->print_cr("Deoptimization traps recorded:"); 1.1723 + #define PRINT_STAT_LINE(name, r) \ 1.1724 + tty->print_cr(" %4d (%4.1f%%) %s", (int)(r), ((r) * 100.0) / total, name); 1.1725 + PRINT_STAT_LINE("total", total); 1.1726 + // For each non-zero entry in the histogram, print the reason, 1.1727 + // the action, and (if specifically known) the type of bytecode. 1.1728 + for (int reason = 0; reason < Reason_LIMIT; reason++) { 1.1729 + for (int action = 0; action < Action_LIMIT; action++) { 1.1730 + juint* cases = _deoptimization_hist[reason][1+action]; 1.1731 + for (int bc_case = 0; bc_case < BC_CASE_LIMIT; bc_case++) { 1.1732 + juint counter = cases[bc_case]; 1.1733 + if (counter != 0) { 1.1734 + char name[1*K]; 1.1735 + Bytecodes::Code bc = (Bytecodes::Code)(counter & LSB_MASK); 1.1736 + if (bc_case == BC_CASE_LIMIT && (int)bc == 0) 1.1737 + bc = Bytecodes::_illegal; 1.1738 + sprintf(name, "%s/%s/%s", 1.1739 + trap_reason_name(reason), 1.1740 + trap_action_name(action), 1.1741 + Bytecodes::is_defined(bc)? Bytecodes::name(bc): "other"); 1.1742 + juint r = counter >> LSB_BITS; 1.1743 + tty->print_cr(" %40s: " UINT32_FORMAT " (%.1f%%)", name, r, (r * 100.0) / total); 1.1744 + account -= r; 1.1745 + } 1.1746 + } 1.1747 + } 1.1748 + } 1.1749 + if (account != 0) { 1.1750 + PRINT_STAT_LINE("unaccounted", account); 1.1751 + } 1.1752 + #undef PRINT_STAT_LINE 1.1753 + if (xtty != NULL) xtty->tail("statistics"); 1.1754 + } 1.1755 +} 1.1756 +#else // COMPILER2 1.1757 + 1.1758 + 1.1759 +// Stubs for C1 only system. 1.1760 +bool Deoptimization::trap_state_is_recompiled(int trap_state) { 1.1761 + return false; 1.1762 +} 1.1763 + 1.1764 +const char* Deoptimization::trap_reason_name(int reason) { 1.1765 + return "unknown"; 1.1766 +} 1.1767 + 1.1768 +void Deoptimization::print_statistics() { 1.1769 + // no output 1.1770 +} 1.1771 + 1.1772 +void 1.1773 +Deoptimization::update_method_data_from_interpreter(methodDataHandle trap_mdo, int trap_bci, int reason) { 1.1774 + // no udpate 1.1775 +} 1.1776 + 1.1777 +int Deoptimization::trap_state_has_reason(int trap_state, int reason) { 1.1778 + return 0; 1.1779 +} 1.1780 + 1.1781 +void Deoptimization::gather_statistics(DeoptReason reason, DeoptAction action, 1.1782 + Bytecodes::Code bc) { 1.1783 + // no update 1.1784 +} 1.1785 + 1.1786 +const char* Deoptimization::format_trap_state(char* buf, size_t buflen, 1.1787 + int trap_state) { 1.1788 + jio_snprintf(buf, buflen, "#%d", trap_state); 1.1789 + return buf; 1.1790 +} 1.1791 + 1.1792 +#endif // COMPILER2