36 #include "memory/oopFactory.hpp"
37 #include "memory/resourceArea.hpp"
38 #include "oops/method.hpp"
39 #include "oops/oop.inline.hpp"
40 #include "oops/verifyOopClosure.hpp"
41 #include "prims/jvmtiThreadState.hpp"
42 #include "runtime/biasedLocking.hpp"
43 #include "runtime/compilationPolicy.hpp"
44 #include "runtime/deoptimization.hpp"
45 #include "runtime/interfaceSupport.hpp"
46 #include "runtime/sharedRuntime.hpp"
47 #include "runtime/signature.hpp"
48 #include "runtime/stubRoutines.hpp"
49 #include "runtime/thread.hpp"
50 #include "runtime/vframe.hpp"
51 #include "runtime/vframeArray.hpp"
52 #include "runtime/vframe_hp.hpp"
53 #include "utilities/events.hpp"
54 #include "utilities/xmlstream.hpp"
55
56 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
57
58 bool DeoptimizationMarker::_is_active = false;
59
60 Deoptimization::UnrollBlock::UnrollBlock(int size_of_deoptimized_frame,
61 int caller_adjustment,
62 int caller_actual_parameters,
63 int number_of_frames,
64 intptr_t* frame_sizes,
65 address* frame_pcs,
66 BasicType return_type) {
67 _size_of_deoptimized_frame = size_of_deoptimized_frame;
68 _caller_adjustment = caller_adjustment;
69 _caller_actual_parameters = caller_actual_parameters;
70 _number_of_frames = number_of_frames;
71 _frame_sizes = frame_sizes;
72 _frame_pcs = frame_pcs;
73 _register_block = NEW_C_HEAP_ARRAY(intptr_t, RegisterMap::reg_count * 2, mtCompiler);
74 _return_type = return_type;
75 _initial_info = 0;
76 // PD (x86 only)
77 _counter_temp = 0;
95 }
96
97
98
99 int Deoptimization::UnrollBlock::size_of_frames() const {
100 // Acount first for the adjustment of the initial frame
101 int result = _caller_adjustment;
102 for (int index = 0; index < number_of_frames(); index++) {
103 result += frame_sizes()[index];
104 }
105 return result;
106 }
107
108
109 void Deoptimization::UnrollBlock::print() {
110 ttyLocker ttyl;
111 tty->print_cr("UnrollBlock");
112 tty->print_cr(" size_of_deoptimized_frame = %d", _size_of_deoptimized_frame);
113 tty->print( " frame_sizes: ");
114 for (int index = 0; index < number_of_frames(); index++) {
115 tty->print("%d ", frame_sizes()[index]);
116 }
117 tty->cr();
118 }
119
120
121 // In order to make fetch_unroll_info work properly with escape
122 // analysis, The method was changed from JRT_LEAF to JRT_BLOCK_ENTRY and
123 // ResetNoHandleMark and HandleMark were removed from it. The actual reallocation
124 // of previously eliminated objects occurs in realloc_objects, which is
125 // called from the method fetch_unroll_info_helper below.
126 JRT_BLOCK_ENTRY(Deoptimization::UnrollBlock*, Deoptimization::fetch_unroll_info(JavaThread* thread))
127 // It is actually ok to allocate handles in a leaf method. It causes no safepoints,
128 // but makes the entry a little slower. There is however a little dance we have to
129 // do in debug mode to get around the NoHandleMark code in the JRT_LEAF macro
130
131 // fetch_unroll_info() is called at the beginning of the deoptimization
132 // handler. Note this fact before we start generating temporary frames
133 // that can confuse an asynchronous stack walker. This counter is
134 // decremented at the end of unpack_frames().
135 thread->inc_in_deopt_handler();
189
190 // The flag return_oop() indicates call sites which return oop
191 // in compiled code. Such sites include java method calls,
192 // runtime calls (for example, used to allocate new objects/arrays
193 // on slow code path) and any other calls generated in compiled code.
194 // It is not guaranteed that we can get such information here only
195 // by analyzing bytecode in deoptimized frames. This is why this flag
196 // is set during method compilation (see Compile::Process_OopMap_Node()).
197 // If the previous frame was popped, we don't have a result.
198 bool save_oop_result = chunk->at(0)->scope()->return_oop() && !thread->popframe_forcing_deopt_reexecution();
199 Handle return_value;
200 if (save_oop_result) {
201 // Reallocation may trigger GC. If deoptimization happened on return from
202 // call which returns oop we need to save it since it is not in oopmap.
203 oop result = deoptee.saved_oop_result(&map);
204 assert(result == NULL || result->is_oop(), "must be oop");
205 return_value = Handle(thread, result);
206 assert(Universe::heap()->is_in_or_null(result), "must be heap pointer");
207 if (TraceDeoptimization) {
208 ttyLocker ttyl;
209 tty->print_cr("SAVED OOP RESULT " INTPTR_FORMAT " in thread " INTPTR_FORMAT, (void *)result, thread);
210 }
211 }
212 if (objects != NULL) {
213 JRT_BLOCK
214 realloc_failures = realloc_objects(thread, &deoptee, objects, THREAD);
215 JRT_END
216 reassign_fields(&deoptee, &map, objects, realloc_failures);
217 #ifndef PRODUCT
218 if (TraceDeoptimization) {
219 ttyLocker ttyl;
220 tty->print_cr("REALLOC OBJECTS in thread " INTPTR_FORMAT, thread);
221 print_objects(objects, realloc_failures);
222 }
223 #endif
224 }
225 if (save_oop_result) {
226 // Restore result.
227 deoptee.set_saved_oop_result(&map, return_value());
228 }
229 }
230 if (EliminateLocks) {
231 #ifndef PRODUCT
232 bool first = true;
233 #endif
234 for (int i = 0; i < chunk->length(); i++) {
235 compiledVFrame* cvf = chunk->at(i);
236 assert (cvf->scope() != NULL,"expect only compiled java frames");
237 GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
238 if (monitors->is_nonempty()) {
239 relock_objects(monitors, thread, realloc_failures);
240 #ifndef PRODUCT
241 if (TraceDeoptimization) {
242 ttyLocker ttyl;
243 for (int j = 0; j < monitors->length(); j++) {
244 MonitorInfo* mi = monitors->at(j);
245 if (mi->eliminated()) {
246 if (first) {
247 first = false;
248 tty->print_cr("RELOCK OBJECTS in thread " INTPTR_FORMAT, thread);
249 }
250 if (mi->owner_is_scalar_replaced()) {
251 Klass* k = java_lang_Class::as_Klass(mi->owner_klass());
252 tty->print_cr(" failed reallocation for klass %s", k->external_name());
253 } else {
254 tty->print_cr(" object <" INTPTR_FORMAT "> locked", (void *)mi->owner());
255 }
256 }
257 }
258 }
259 #endif
260 }
261 }
262 }
263 }
264 #endif // COMPILER2
265 // Ensure that no safepoint is taken after pointers have been stored
266 // in fields of rematerialized objects. If a safepoint occurs from here on
267 // out the java state residing in the vframeArray will be missed.
268 No_Safepoint_Verifier no_safepoint;
269
270 vframeArray* array = create_vframeArray(thread, deoptee, &map, chunk, realloc_failures);
271 #ifdef COMPILER2
272 if (realloc_failures) {
273 pop_frames_failed_reallocs(thread, array);
274 }
533 JRT_LEAF(BasicType, Deoptimization::unpack_frames(JavaThread* thread, int exec_mode))
534
535 // We are already active int he special DeoptResourceMark any ResourceObj's we
536 // allocate will be freed at the end of the routine.
537
538 // It is actually ok to allocate handles in a leaf method. It causes no safepoints,
539 // but makes the entry a little slower. There is however a little dance we have to
540 // do in debug mode to get around the NoHandleMark code in the JRT_LEAF macro
541 ResetNoHandleMark rnhm; // No-op in release/product versions
542 HandleMark hm;
543
544 frame stub_frame = thread->last_frame();
545
546 // Since the frame to unpack is the top frame of this thread, the vframe_array_head
547 // must point to the vframeArray for the unpack frame.
548 vframeArray* array = thread->vframe_array_head();
549
550 #ifndef PRODUCT
551 if (TraceDeoptimization) {
552 ttyLocker ttyl;
553 tty->print_cr("DEOPT UNPACKING thread " INTPTR_FORMAT " vframeArray " INTPTR_FORMAT " mode %d", thread, array, exec_mode);
554 }
555 #endif
556 Events::log(thread, "DEOPT UNPACKING pc=" INTPTR_FORMAT " sp=" INTPTR_FORMAT " mode %d",
557 stub_frame.pc(), stub_frame.sp(), exec_mode);
558
559 UnrollBlock* info = array->unroll_block();
560
561 // Unpack the interpreter frames and any adapter frame (c2 only) we might create.
562 array->unpack_to_stack(stub_frame, exec_mode, info->caller_actual_parameters());
563
564 BasicType bt = info->return_type();
565
566 // If we have an exception pending, claim that the return type is an oop
567 // so the deopt_blob does not overwrite the exception_oop.
568
569 if (exec_mode == Unpack_exception)
570 bt = T_OBJECT;
571
572 // Cleanup thread deopt data
573 cleanup_deopt_info(thread, array);
574
575 #ifndef PRODUCT
576 if (VerifyStack) {
577 ResourceMark res_mark;
673 top_frame_expression_stack_adjustment))) ||
674 (is_top_frame && (exec_mode == Unpack_exception) && iframe->interpreter_frame_expression_stack_size() == 0) ||
675 (is_top_frame && (exec_mode == Unpack_uncommon_trap || exec_mode == Unpack_reexecute || el->should_reexecute()) &&
676 (iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + cur_invoke_parameter_size))
677 )) {
678 ttyLocker ttyl;
679
680 // Print out some information that will help us debug the problem
681 tty->print_cr("Wrong number of expression stack elements during deoptimization");
682 tty->print_cr(" Error occurred while verifying frame %d (0..%d, 0 is topmost)", i, cur_array->frames() - 1);
683 tty->print_cr(" Fabricated interpreter frame had %d expression stack elements",
684 iframe->interpreter_frame_expression_stack_size());
685 tty->print_cr(" Interpreter oop map had %d expression stack elements", mask.expression_stack_size());
686 tty->print_cr(" try_next_mask = %d", try_next_mask);
687 tty->print_cr(" next_mask_expression_stack_size = %d", next_mask_expression_stack_size);
688 tty->print_cr(" callee_size_of_parameters = %d", callee_size_of_parameters);
689 tty->print_cr(" callee_max_locals = %d", callee_max_locals);
690 tty->print_cr(" top_frame_expression_stack_adjustment = %d", top_frame_expression_stack_adjustment);
691 tty->print_cr(" exec_mode = %d", exec_mode);
692 tty->print_cr(" cur_invoke_parameter_size = %d", cur_invoke_parameter_size);
693 tty->print_cr(" Thread = " INTPTR_FORMAT ", thread ID = " UINTX_FORMAT, thread, thread->osthread()->thread_id());
694 tty->print_cr(" Interpreted frames:");
695 for (int k = 0; k < cur_array->frames(); k++) {
696 vframeArrayElement* el = cur_array->element(k);
697 tty->print_cr(" %s (bci %d)", el->method()->name_and_sig_as_C_string(), el->bci());
698 }
699 cur_array->print_on_2(tty);
700 guarantee(false, "wrong number of expression stack elements during deopt");
701 }
702 VerifyOopClosure verify;
703 iframe->oops_interpreted_do(&verify, NULL, &rm, false);
704 callee_size_of_parameters = mh->size_of_parameters();
705 callee_max_locals = mh->max_locals();
706 is_top_frame = false;
707 }
708 }
709 #endif /* !PRODUCT */
710
711
712 return bt;
713 JRT_END
949 }
950 BasicLock* lock = mon_info->lock();
951 ObjectSynchronizer::slow_enter(obj, lock, thread);
952 assert(mon_info->owner()->is_locked(), "object must be locked now");
953 }
954 }
955 }
956 }
957
958
959 #ifndef PRODUCT
960 // print information about reallocated objects
961 void Deoptimization::print_objects(GrowableArray<ScopeValue*>* objects, bool realloc_failures) {
962 fieldDescriptor fd;
963
964 for (int i = 0; i < objects->length(); i++) {
965 ObjectValue* sv = (ObjectValue*) objects->at(i);
966 KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()()));
967 Handle obj = sv->value();
968
969 tty->print(" object <" INTPTR_FORMAT "> of type ", (void *)sv->value()());
970 k->print_value();
971 assert(obj.not_null() || realloc_failures, "reallocation was missed");
972 if (obj.is_null()) {
973 tty->print(" allocation failed");
974 } else {
975 tty->print(" allocated (%d bytes)", obj->size() * HeapWordSize);
976 }
977 tty->cr();
978
979 if (Verbose && !obj.is_null()) {
980 k->oop_print_on(obj(), tty);
981 }
982 }
983 }
984 #endif
985 #endif // COMPILER2
986
987 vframeArray* Deoptimization::create_vframeArray(JavaThread* thread, frame fr, RegisterMap *reg_map, GrowableArray<compiledVFrame*>* chunk, bool realloc_failures) {
988 Events::log(thread, "DEOPT PACKING pc=" INTPTR_FORMAT " sp=" INTPTR_FORMAT, fr.pc(), fr.sp());
989
990 #ifndef PRODUCT
991 if (TraceDeoptimization) {
992 ttyLocker ttyl;
993 tty->print("DEOPT PACKING thread " INTPTR_FORMAT " ", thread);
994 fr.print_on(tty);
995 tty->print_cr(" Virtual frames (innermost first):");
996 for (int index = 0; index < chunk->length(); index++) {
997 compiledVFrame* vf = chunk->at(index);
998 tty->print(" %2d - ", index);
999 vf->print_value();
1000 int bci = chunk->at(index)->raw_bci();
1001 const char* code_name;
1002 if (bci == SynchronizationEntryBCI) {
1003 code_name = "sync entry";
1004 } else {
1005 Bytecodes::Code code = vf->method()->code_at(bci);
1006 code_name = Bytecodes::name(code);
1007 }
1008 tty->print(" - %s", code_name);
1009 tty->print_cr(" @ bci %d ", bci);
1010 if (Verbose) {
1011 vf->print();
1012 tty->cr();
1013 }
1018 // Register map for next frame (used for stack crawl). We capture
1019 // the state of the deopt'ing frame's caller. Thus if we need to
1020 // stuff a C2I adapter we can properly fill in the callee-save
1021 // register locations.
1022 frame caller = fr.sender(reg_map);
1023 int frame_size = caller.sp() - fr.sp();
1024
1025 frame sender = caller;
1026
1027 // Since the Java thread being deoptimized will eventually adjust it's own stack,
1028 // the vframeArray containing the unpacking information is allocated in the C heap.
1029 // For Compiler1, the caller of the deoptimized frame is saved for use by unpack_frames().
1030 vframeArray* array = vframeArray::allocate(thread, frame_size, chunk, reg_map, sender, caller, fr, realloc_failures);
1031
1032 // Compare the vframeArray to the collected vframes
1033 assert(array->structural_compare(thread, chunk), "just checking");
1034
1035 #ifndef PRODUCT
1036 if (TraceDeoptimization) {
1037 ttyLocker ttyl;
1038 tty->print_cr(" Created vframeArray " INTPTR_FORMAT, array);
1039 }
1040 #endif // PRODUCT
1041
1042 return array;
1043 }
1044
1045 #ifdef COMPILER2
1046 void Deoptimization::pop_frames_failed_reallocs(JavaThread* thread, vframeArray* array) {
1047 // Reallocation of some scalar replaced objects failed. Record
1048 // that we need to pop all the interpreter frames for the
1049 // deoptimized compiled frame.
1050 assert(thread->frames_to_pop_failed_realloc() == 0, "missed frames to pop?");
1051 thread->set_frames_to_pop_failed_realloc(array->frames());
1052 // Unlock all monitors here otherwise the interpreter will see a
1053 // mix of locked and unlocked monitors (because of failed
1054 // reallocations of synchronized objects) and be confused.
1055 for (int i = 0; i < array->frames(); i++) {
1056 MonitorChunk* monitors = array->element(i)->monitors();
1057 if (monitors != NULL) {
1058 for (int j = 0; j < monitors->number_of_monitors(); j++) {
1280
1281 JRT_ENTRY(void, Deoptimization::uncommon_trap_inner(JavaThread* thread, jint trap_request)) {
1282 HandleMark hm;
1283
1284 // uncommon_trap() is called at the beginning of the uncommon trap
1285 // handler. Note this fact before we start generating temporary frames
1286 // that can confuse an asynchronous stack walker. This counter is
1287 // decremented at the end of unpack_frames().
1288 thread->inc_in_deopt_handler();
1289
1290 // We need to update the map if we have biased locking.
1291 RegisterMap reg_map(thread, UseBiasedLocking);
1292 frame stub_frame = thread->last_frame();
1293 frame fr = stub_frame.sender(®_map);
1294 // Make sure the calling nmethod is not getting deoptimized and removed
1295 // before we are done with it.
1296 nmethodLocker nl(fr.pc());
1297
1298 // Log a message
1299 Events::log(thread, "Uncommon trap: trap_request=" PTR32_FORMAT " fr.pc=" INTPTR_FORMAT,
1300 trap_request, fr.pc());
1301
1302 {
1303 ResourceMark rm;
1304
1305 // Revoke biases of any monitors in the frame to ensure we can migrate them
1306 revoke_biases_of_monitors(thread, fr, ®_map);
1307
1308 DeoptReason reason = trap_request_reason(trap_request);
1309 DeoptAction action = trap_request_action(trap_request);
1310 jint unloaded_class_index = trap_request_index(trap_request); // CP idx or -1
1311
1312 vframe* vf = vframe::new_vframe(&fr, ®_map, thread);
1313 compiledVFrame* cvf = compiledVFrame::cast(vf);
1314
1315 nmethod* nm = cvf->code();
1316
1317 ScopeDesc* trap_scope = cvf->scope();
1318 methodHandle trap_method = trap_scope->method();
1319 int trap_bci = trap_scope->bci();
1320 Bytecodes::Code trap_bc = trap_method->java_code_at(trap_bci);
1321
1322 // Record this event in the histogram.
1323 gather_statistics(reason, action, trap_bc);
1324
1325 // Ensure that we can record deopt. history:
1326 // Need MDO to record RTM code generation state.
1327 bool create_if_missing = ProfileTraps || UseCodeAging RTM_OPT_ONLY( || UseRTMLocking );
1328
1329 MethodData* trap_mdo =
1330 get_method_data(thread, trap_method, create_if_missing);
1331
1332 // Log a message
1333 Events::log_deopt_message(thread, "Uncommon trap: reason=%s action=%s pc=" INTPTR_FORMAT " method=%s @ %d",
1334 trap_reason_name(reason), trap_action_name(action), fr.pc(),
1335 trap_method->name_and_sig_as_C_string(), trap_bci);
1336
1337 // Print a bunch of diagnostics, if requested.
1338 if (TraceDeoptimization || LogCompilation) {
1339 ResourceMark rm;
1340 ttyLocker ttyl;
1341 char buf[100];
1342 if (xtty != NULL) {
1343 xtty->begin_head("uncommon_trap thread='" UINTX_FORMAT "' %s",
1344 os::current_thread_id(),
1345 format_trap_request(buf, sizeof(buf), trap_request));
1346 nm->log_identity(xtty);
1347 }
1348 Symbol* class_name = NULL;
1349 bool unresolved = false;
1350 if (unloaded_class_index >= 0) {
1351 constantPoolHandle constants (THREAD, trap_method->constants());
1352 if (constants->tag_at(unloaded_class_index).is_unresolved_klass()) {
1353 class_name = constants->klass_name_at(unloaded_class_index);
1354 unresolved = true;
1369 xtty->print(" count='%d'", dcnt);
1370 ProfileData* pdata = trap_mdo->bci_to_data(trap_bci);
1371 int dos = (pdata == NULL)? 0: pdata->trap_state();
1372 if (dos != 0) {
1373 xtty->print(" state='%s'", format_trap_state(buf, sizeof(buf), dos));
1374 if (trap_state_is_recompiled(dos)) {
1375 int recnt2 = trap_mdo->overflow_recompile_count();
1376 if (recnt2 != 0)
1377 xtty->print(" recompiles2='%d'", recnt2);
1378 }
1379 }
1380 }
1381 if (xtty != NULL) {
1382 xtty->stamp();
1383 xtty->end_head();
1384 }
1385 if (TraceDeoptimization) { // make noise on the tty
1386 tty->print("Uncommon trap occurred in");
1387 nm->method()->print_short_name(tty);
1388 tty->print(" (@" INTPTR_FORMAT ") thread=" UINTX_FORMAT " reason=%s action=%s unloaded_class_index=%d",
1389 fr.pc(),
1390 os::current_thread_id(),
1391 trap_reason_name(reason),
1392 trap_action_name(action),
1393 unloaded_class_index);
1394 if (class_name != NULL) {
1395 tty->print(unresolved ? " unresolved class: " : " symbol: ");
1396 class_name->print_symbol_on(tty);
1397 }
1398 tty->cr();
1399 }
1400 if (xtty != NULL) {
1401 // Log the precise location of the trap.
1402 for (ScopeDesc* sd = trap_scope; ; sd = sd->sender()) {
1403 xtty->begin_elem("jvms bci='%d'", sd->bci());
1404 xtty->method(sd->method());
1405 xtty->end_elem();
1406 if (sd->is_top()) break;
1407 }
1408 xtty->tail("uncommon_trap");
1409 }
|
36 #include "memory/oopFactory.hpp"
37 #include "memory/resourceArea.hpp"
38 #include "oops/method.hpp"
39 #include "oops/oop.inline.hpp"
40 #include "oops/verifyOopClosure.hpp"
41 #include "prims/jvmtiThreadState.hpp"
42 #include "runtime/biasedLocking.hpp"
43 #include "runtime/compilationPolicy.hpp"
44 #include "runtime/deoptimization.hpp"
45 #include "runtime/interfaceSupport.hpp"
46 #include "runtime/sharedRuntime.hpp"
47 #include "runtime/signature.hpp"
48 #include "runtime/stubRoutines.hpp"
49 #include "runtime/thread.hpp"
50 #include "runtime/vframe.hpp"
51 #include "runtime/vframeArray.hpp"
52 #include "runtime/vframe_hp.hpp"
53 #include "utilities/events.hpp"
54 #include "utilities/xmlstream.hpp"
55
56 bool DeoptimizationMarker::_is_active = false;
57
58 Deoptimization::UnrollBlock::UnrollBlock(int size_of_deoptimized_frame,
59 int caller_adjustment,
60 int caller_actual_parameters,
61 int number_of_frames,
62 intptr_t* frame_sizes,
63 address* frame_pcs,
64 BasicType return_type) {
65 _size_of_deoptimized_frame = size_of_deoptimized_frame;
66 _caller_adjustment = caller_adjustment;
67 _caller_actual_parameters = caller_actual_parameters;
68 _number_of_frames = number_of_frames;
69 _frame_sizes = frame_sizes;
70 _frame_pcs = frame_pcs;
71 _register_block = NEW_C_HEAP_ARRAY(intptr_t, RegisterMap::reg_count * 2, mtCompiler);
72 _return_type = return_type;
73 _initial_info = 0;
74 // PD (x86 only)
75 _counter_temp = 0;
93 }
94
95
96
97 int Deoptimization::UnrollBlock::size_of_frames() const {
98 // Acount first for the adjustment of the initial frame
99 int result = _caller_adjustment;
100 for (int index = 0; index < number_of_frames(); index++) {
101 result += frame_sizes()[index];
102 }
103 return result;
104 }
105
106
107 void Deoptimization::UnrollBlock::print() {
108 ttyLocker ttyl;
109 tty->print_cr("UnrollBlock");
110 tty->print_cr(" size_of_deoptimized_frame = %d", _size_of_deoptimized_frame);
111 tty->print( " frame_sizes: ");
112 for (int index = 0; index < number_of_frames(); index++) {
113 tty->print(INTX_FORMAT " ", frame_sizes()[index]);
114 }
115 tty->cr();
116 }
117
118
119 // In order to make fetch_unroll_info work properly with escape
120 // analysis, The method was changed from JRT_LEAF to JRT_BLOCK_ENTRY and
121 // ResetNoHandleMark and HandleMark were removed from it. The actual reallocation
122 // of previously eliminated objects occurs in realloc_objects, which is
123 // called from the method fetch_unroll_info_helper below.
124 JRT_BLOCK_ENTRY(Deoptimization::UnrollBlock*, Deoptimization::fetch_unroll_info(JavaThread* thread))
125 // It is actually ok to allocate handles in a leaf method. It causes no safepoints,
126 // but makes the entry a little slower. There is however a little dance we have to
127 // do in debug mode to get around the NoHandleMark code in the JRT_LEAF macro
128
129 // fetch_unroll_info() is called at the beginning of the deoptimization
130 // handler. Note this fact before we start generating temporary frames
131 // that can confuse an asynchronous stack walker. This counter is
132 // decremented at the end of unpack_frames().
133 thread->inc_in_deopt_handler();
187
188 // The flag return_oop() indicates call sites which return oop
189 // in compiled code. Such sites include java method calls,
190 // runtime calls (for example, used to allocate new objects/arrays
191 // on slow code path) and any other calls generated in compiled code.
192 // It is not guaranteed that we can get such information here only
193 // by analyzing bytecode in deoptimized frames. This is why this flag
194 // is set during method compilation (see Compile::Process_OopMap_Node()).
195 // If the previous frame was popped, we don't have a result.
196 bool save_oop_result = chunk->at(0)->scope()->return_oop() && !thread->popframe_forcing_deopt_reexecution();
197 Handle return_value;
198 if (save_oop_result) {
199 // Reallocation may trigger GC. If deoptimization happened on return from
200 // call which returns oop we need to save it since it is not in oopmap.
201 oop result = deoptee.saved_oop_result(&map);
202 assert(result == NULL || result->is_oop(), "must be oop");
203 return_value = Handle(thread, result);
204 assert(Universe::heap()->is_in_or_null(result), "must be heap pointer");
205 if (TraceDeoptimization) {
206 ttyLocker ttyl;
207 tty->print_cr("SAVED OOP RESULT " INTPTR_FORMAT " in thread " INTPTR_FORMAT, p2i(result), p2i(thread));
208 }
209 }
210 if (objects != NULL) {
211 JRT_BLOCK
212 realloc_failures = realloc_objects(thread, &deoptee, objects, THREAD);
213 JRT_END
214 reassign_fields(&deoptee, &map, objects, realloc_failures);
215 #ifndef PRODUCT
216 if (TraceDeoptimization) {
217 ttyLocker ttyl;
218 tty->print_cr("REALLOC OBJECTS in thread " INTPTR_FORMAT, p2i(thread));
219 print_objects(objects, realloc_failures);
220 }
221 #endif
222 }
223 if (save_oop_result) {
224 // Restore result.
225 deoptee.set_saved_oop_result(&map, return_value());
226 }
227 }
228 if (EliminateLocks) {
229 #ifndef PRODUCT
230 bool first = true;
231 #endif
232 for (int i = 0; i < chunk->length(); i++) {
233 compiledVFrame* cvf = chunk->at(i);
234 assert (cvf->scope() != NULL,"expect only compiled java frames");
235 GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
236 if (monitors->is_nonempty()) {
237 relock_objects(monitors, thread, realloc_failures);
238 #ifndef PRODUCT
239 if (TraceDeoptimization) {
240 ttyLocker ttyl;
241 for (int j = 0; j < monitors->length(); j++) {
242 MonitorInfo* mi = monitors->at(j);
243 if (mi->eliminated()) {
244 if (first) {
245 first = false;
246 tty->print_cr("RELOCK OBJECTS in thread " INTPTR_FORMAT, p2i(thread));
247 }
248 if (mi->owner_is_scalar_replaced()) {
249 Klass* k = java_lang_Class::as_Klass(mi->owner_klass());
250 tty->print_cr(" failed reallocation for klass %s", k->external_name());
251 } else {
252 tty->print_cr(" object <" INTPTR_FORMAT "> locked", p2i(mi->owner()));
253 }
254 }
255 }
256 }
257 #endif
258 }
259 }
260 }
261 }
262 #endif // COMPILER2
263 // Ensure that no safepoint is taken after pointers have been stored
264 // in fields of rematerialized objects. If a safepoint occurs from here on
265 // out the java state residing in the vframeArray will be missed.
266 No_Safepoint_Verifier no_safepoint;
267
268 vframeArray* array = create_vframeArray(thread, deoptee, &map, chunk, realloc_failures);
269 #ifdef COMPILER2
270 if (realloc_failures) {
271 pop_frames_failed_reallocs(thread, array);
272 }
531 JRT_LEAF(BasicType, Deoptimization::unpack_frames(JavaThread* thread, int exec_mode))
532
533 // We are already active int he special DeoptResourceMark any ResourceObj's we
534 // allocate will be freed at the end of the routine.
535
536 // It is actually ok to allocate handles in a leaf method. It causes no safepoints,
537 // but makes the entry a little slower. There is however a little dance we have to
538 // do in debug mode to get around the NoHandleMark code in the JRT_LEAF macro
539 ResetNoHandleMark rnhm; // No-op in release/product versions
540 HandleMark hm;
541
542 frame stub_frame = thread->last_frame();
543
544 // Since the frame to unpack is the top frame of this thread, the vframe_array_head
545 // must point to the vframeArray for the unpack frame.
546 vframeArray* array = thread->vframe_array_head();
547
548 #ifndef PRODUCT
549 if (TraceDeoptimization) {
550 ttyLocker ttyl;
551 tty->print_cr("DEOPT UNPACKING thread " INTPTR_FORMAT " vframeArray " INTPTR_FORMAT " mode %d",
552 p2i(thread), p2i(array), exec_mode);
553 }
554 #endif
555 Events::log(thread, "DEOPT UNPACKING pc=" INTPTR_FORMAT " sp=" INTPTR_FORMAT " mode %d",
556 p2i(stub_frame.pc()), p2i(stub_frame.sp()), exec_mode);
557
558 UnrollBlock* info = array->unroll_block();
559
560 // Unpack the interpreter frames and any adapter frame (c2 only) we might create.
561 array->unpack_to_stack(stub_frame, exec_mode, info->caller_actual_parameters());
562
563 BasicType bt = info->return_type();
564
565 // If we have an exception pending, claim that the return type is an oop
566 // so the deopt_blob does not overwrite the exception_oop.
567
568 if (exec_mode == Unpack_exception)
569 bt = T_OBJECT;
570
571 // Cleanup thread deopt data
572 cleanup_deopt_info(thread, array);
573
574 #ifndef PRODUCT
575 if (VerifyStack) {
576 ResourceMark res_mark;
672 top_frame_expression_stack_adjustment))) ||
673 (is_top_frame && (exec_mode == Unpack_exception) && iframe->interpreter_frame_expression_stack_size() == 0) ||
674 (is_top_frame && (exec_mode == Unpack_uncommon_trap || exec_mode == Unpack_reexecute || el->should_reexecute()) &&
675 (iframe->interpreter_frame_expression_stack_size() == mask.expression_stack_size() + cur_invoke_parameter_size))
676 )) {
677 ttyLocker ttyl;
678
679 // Print out some information that will help us debug the problem
680 tty->print_cr("Wrong number of expression stack elements during deoptimization");
681 tty->print_cr(" Error occurred while verifying frame %d (0..%d, 0 is topmost)", i, cur_array->frames() - 1);
682 tty->print_cr(" Fabricated interpreter frame had %d expression stack elements",
683 iframe->interpreter_frame_expression_stack_size());
684 tty->print_cr(" Interpreter oop map had %d expression stack elements", mask.expression_stack_size());
685 tty->print_cr(" try_next_mask = %d", try_next_mask);
686 tty->print_cr(" next_mask_expression_stack_size = %d", next_mask_expression_stack_size);
687 tty->print_cr(" callee_size_of_parameters = %d", callee_size_of_parameters);
688 tty->print_cr(" callee_max_locals = %d", callee_max_locals);
689 tty->print_cr(" top_frame_expression_stack_adjustment = %d", top_frame_expression_stack_adjustment);
690 tty->print_cr(" exec_mode = %d", exec_mode);
691 tty->print_cr(" cur_invoke_parameter_size = %d", cur_invoke_parameter_size);
692 tty->print_cr(" Thread = " INTPTR_FORMAT ", thread ID = %d", p2i(thread), thread->osthread()->thread_id());
693 tty->print_cr(" Interpreted frames:");
694 for (int k = 0; k < cur_array->frames(); k++) {
695 vframeArrayElement* el = cur_array->element(k);
696 tty->print_cr(" %s (bci %d)", el->method()->name_and_sig_as_C_string(), el->bci());
697 }
698 cur_array->print_on_2(tty);
699 guarantee(false, "wrong number of expression stack elements during deopt");
700 }
701 VerifyOopClosure verify;
702 iframe->oops_interpreted_do(&verify, NULL, &rm, false);
703 callee_size_of_parameters = mh->size_of_parameters();
704 callee_max_locals = mh->max_locals();
705 is_top_frame = false;
706 }
707 }
708 #endif /* !PRODUCT */
709
710
711 return bt;
712 JRT_END
948 }
949 BasicLock* lock = mon_info->lock();
950 ObjectSynchronizer::slow_enter(obj, lock, thread);
951 assert(mon_info->owner()->is_locked(), "object must be locked now");
952 }
953 }
954 }
955 }
956
957
958 #ifndef PRODUCT
959 // print information about reallocated objects
960 void Deoptimization::print_objects(GrowableArray<ScopeValue*>* objects, bool realloc_failures) {
961 fieldDescriptor fd;
962
963 for (int i = 0; i < objects->length(); i++) {
964 ObjectValue* sv = (ObjectValue*) objects->at(i);
965 KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()()));
966 Handle obj = sv->value();
967
968 tty->print(" object <" INTPTR_FORMAT "> of type ", p2i(sv->value()()));
969 k->print_value();
970 assert(obj.not_null() || realloc_failures, "reallocation was missed");
971 if (obj.is_null()) {
972 tty->print(" allocation failed");
973 } else {
974 tty->print(" allocated (%d bytes)", obj->size() * HeapWordSize);
975 }
976 tty->cr();
977
978 if (Verbose && !obj.is_null()) {
979 k->oop_print_on(obj(), tty);
980 }
981 }
982 }
983 #endif
984 #endif // COMPILER2
985
986 vframeArray* Deoptimization::create_vframeArray(JavaThread* thread, frame fr, RegisterMap *reg_map, GrowableArray<compiledVFrame*>* chunk, bool realloc_failures) {
987 Events::log(thread, "DEOPT PACKING pc=" INTPTR_FORMAT " sp=" INTPTR_FORMAT, p2i(fr.pc()), p2i(fr.sp()));
988
989 #ifndef PRODUCT
990 if (TraceDeoptimization) {
991 ttyLocker ttyl;
992 tty->print("DEOPT PACKING thread " INTPTR_FORMAT " ", p2i(thread));
993 fr.print_on(tty);
994 tty->print_cr(" Virtual frames (innermost first):");
995 for (int index = 0; index < chunk->length(); index++) {
996 compiledVFrame* vf = chunk->at(index);
997 tty->print(" %2d - ", index);
998 vf->print_value();
999 int bci = chunk->at(index)->raw_bci();
1000 const char* code_name;
1001 if (bci == SynchronizationEntryBCI) {
1002 code_name = "sync entry";
1003 } else {
1004 Bytecodes::Code code = vf->method()->code_at(bci);
1005 code_name = Bytecodes::name(code);
1006 }
1007 tty->print(" - %s", code_name);
1008 tty->print_cr(" @ bci %d ", bci);
1009 if (Verbose) {
1010 vf->print();
1011 tty->cr();
1012 }
1017 // Register map for next frame (used for stack crawl). We capture
1018 // the state of the deopt'ing frame's caller. Thus if we need to
1019 // stuff a C2I adapter we can properly fill in the callee-save
1020 // register locations.
1021 frame caller = fr.sender(reg_map);
1022 int frame_size = caller.sp() - fr.sp();
1023
1024 frame sender = caller;
1025
1026 // Since the Java thread being deoptimized will eventually adjust it's own stack,
1027 // the vframeArray containing the unpacking information is allocated in the C heap.
1028 // For Compiler1, the caller of the deoptimized frame is saved for use by unpack_frames().
1029 vframeArray* array = vframeArray::allocate(thread, frame_size, chunk, reg_map, sender, caller, fr, realloc_failures);
1030
1031 // Compare the vframeArray to the collected vframes
1032 assert(array->structural_compare(thread, chunk), "just checking");
1033
1034 #ifndef PRODUCT
1035 if (TraceDeoptimization) {
1036 ttyLocker ttyl;
1037 tty->print_cr(" Created vframeArray " INTPTR_FORMAT, p2i(array));
1038 }
1039 #endif // PRODUCT
1040
1041 return array;
1042 }
1043
1044 #ifdef COMPILER2
1045 void Deoptimization::pop_frames_failed_reallocs(JavaThread* thread, vframeArray* array) {
1046 // Reallocation of some scalar replaced objects failed. Record
1047 // that we need to pop all the interpreter frames for the
1048 // deoptimized compiled frame.
1049 assert(thread->frames_to_pop_failed_realloc() == 0, "missed frames to pop?");
1050 thread->set_frames_to_pop_failed_realloc(array->frames());
1051 // Unlock all monitors here otherwise the interpreter will see a
1052 // mix of locked and unlocked monitors (because of failed
1053 // reallocations of synchronized objects) and be confused.
1054 for (int i = 0; i < array->frames(); i++) {
1055 MonitorChunk* monitors = array->element(i)->monitors();
1056 if (monitors != NULL) {
1057 for (int j = 0; j < monitors->number_of_monitors(); j++) {
1279
1280 JRT_ENTRY(void, Deoptimization::uncommon_trap_inner(JavaThread* thread, jint trap_request)) {
1281 HandleMark hm;
1282
1283 // uncommon_trap() is called at the beginning of the uncommon trap
1284 // handler. Note this fact before we start generating temporary frames
1285 // that can confuse an asynchronous stack walker. This counter is
1286 // decremented at the end of unpack_frames().
1287 thread->inc_in_deopt_handler();
1288
1289 // We need to update the map if we have biased locking.
1290 RegisterMap reg_map(thread, UseBiasedLocking);
1291 frame stub_frame = thread->last_frame();
1292 frame fr = stub_frame.sender(®_map);
1293 // Make sure the calling nmethod is not getting deoptimized and removed
1294 // before we are done with it.
1295 nmethodLocker nl(fr.pc());
1296
1297 // Log a message
1298 Events::log(thread, "Uncommon trap: trap_request=" PTR32_FORMAT " fr.pc=" INTPTR_FORMAT,
1299 trap_request, p2i(fr.pc()));
1300
1301 {
1302 ResourceMark rm;
1303
1304 // Revoke biases of any monitors in the frame to ensure we can migrate them
1305 revoke_biases_of_monitors(thread, fr, ®_map);
1306
1307 DeoptReason reason = trap_request_reason(trap_request);
1308 DeoptAction action = trap_request_action(trap_request);
1309 jint unloaded_class_index = trap_request_index(trap_request); // CP idx or -1
1310
1311 vframe* vf = vframe::new_vframe(&fr, ®_map, thread);
1312 compiledVFrame* cvf = compiledVFrame::cast(vf);
1313
1314 nmethod* nm = cvf->code();
1315
1316 ScopeDesc* trap_scope = cvf->scope();
1317 methodHandle trap_method = trap_scope->method();
1318 int trap_bci = trap_scope->bci();
1319 Bytecodes::Code trap_bc = trap_method->java_code_at(trap_bci);
1320
1321 // Record this event in the histogram.
1322 gather_statistics(reason, action, trap_bc);
1323
1324 // Ensure that we can record deopt. history:
1325 // Need MDO to record RTM code generation state.
1326 bool create_if_missing = ProfileTraps || UseCodeAging RTM_OPT_ONLY( || UseRTMLocking );
1327
1328 MethodData* trap_mdo =
1329 get_method_data(thread, trap_method, create_if_missing);
1330
1331 // Log a message
1332 Events::log_deopt_message(thread, "Uncommon trap: reason=%s action=%s pc=" INTPTR_FORMAT " method=%s @ %d",
1333 trap_reason_name(reason), trap_action_name(action), p2i(fr.pc()),
1334 trap_method->name_and_sig_as_C_string(), trap_bci);
1335
1336 // Print a bunch of diagnostics, if requested.
1337 if (TraceDeoptimization || LogCompilation) {
1338 ResourceMark rm;
1339 ttyLocker ttyl;
1340 char buf[100];
1341 if (xtty != NULL) {
1342 xtty->begin_head("uncommon_trap thread='" UINTX_FORMAT "' %s",
1343 os::current_thread_id(),
1344 format_trap_request(buf, sizeof(buf), trap_request));
1345 nm->log_identity(xtty);
1346 }
1347 Symbol* class_name = NULL;
1348 bool unresolved = false;
1349 if (unloaded_class_index >= 0) {
1350 constantPoolHandle constants (THREAD, trap_method->constants());
1351 if (constants->tag_at(unloaded_class_index).is_unresolved_klass()) {
1352 class_name = constants->klass_name_at(unloaded_class_index);
1353 unresolved = true;
1368 xtty->print(" count='%d'", dcnt);
1369 ProfileData* pdata = trap_mdo->bci_to_data(trap_bci);
1370 int dos = (pdata == NULL)? 0: pdata->trap_state();
1371 if (dos != 0) {
1372 xtty->print(" state='%s'", format_trap_state(buf, sizeof(buf), dos));
1373 if (trap_state_is_recompiled(dos)) {
1374 int recnt2 = trap_mdo->overflow_recompile_count();
1375 if (recnt2 != 0)
1376 xtty->print(" recompiles2='%d'", recnt2);
1377 }
1378 }
1379 }
1380 if (xtty != NULL) {
1381 xtty->stamp();
1382 xtty->end_head();
1383 }
1384 if (TraceDeoptimization) { // make noise on the tty
1385 tty->print("Uncommon trap occurred in");
1386 nm->method()->print_short_name(tty);
1387 tty->print(" (@" INTPTR_FORMAT ") thread=" UINTX_FORMAT " reason=%s action=%s unloaded_class_index=%d",
1388 p2i(fr.pc()),
1389 os::current_thread_id(),
1390 trap_reason_name(reason),
1391 trap_action_name(action),
1392 unloaded_class_index);
1393 if (class_name != NULL) {
1394 tty->print(unresolved ? " unresolved class: " : " symbol: ");
1395 class_name->print_symbol_on(tty);
1396 }
1397 tty->cr();
1398 }
1399 if (xtty != NULL) {
1400 // Log the precise location of the trap.
1401 for (ScopeDesc* sd = trap_scope; ; sd = sd->sender()) {
1402 xtty->begin_elem("jvms bci='%d'", sd->bci());
1403 xtty->method(sd->method());
1404 xtty->end_elem();
1405 if (sd->is_top()) break;
1406 }
1407 xtty->tail("uncommon_trap");
1408 }
|