< prev index next >

src/hotspot/share/runtime/deoptimization.cpp

Print this page
rev 54697 : imported patch 8221734-v2-merge


 757             vframeArrayElement* el = cur_array->element(k);
 758             tty->print_cr("    %s (bci %d)", el->method()->name_and_sig_as_C_string(), el->bci());
 759           }
 760           cur_array->print_on_2(tty);
 761         } // release tty lock before calling guarantee
 762         guarantee(false, "wrong number of expression stack elements during deopt");
 763       }
 764       VerifyOopClosure verify;
 765       iframe->oops_interpreted_do(&verify, &rm, false);
 766       callee_size_of_parameters = mh->size_of_parameters();
 767       callee_max_locals = mh->max_locals();
 768       is_top_frame = false;
 769     }
 770   }
 771 #endif /* !PRODUCT */
 772 
 773 
 774   return bt;
 775 JRT_END
 776 














 777 
 778 int Deoptimization::deoptimize_dependents() {
 779   Threads::deoptimized_wrt_marked_nmethods();
 780   return 0;











 781 }
 782 
 783 Deoptimization::DeoptAction Deoptimization::_unloaded_action
 784   = Deoptimization::Action_reinterpret;
 785 
 786 #if COMPILER2_OR_JVMCI
 787 bool Deoptimization::realloc_objects(JavaThread* thread, frame* fr, GrowableArray<ScopeValue*>* objects, TRAPS) {
 788   Handle pending_exception(THREAD, thread->pending_exception());
 789   const char* exception_file = thread->exception_file();
 790   int exception_line = thread->exception_line();
 791   thread->clear_pending_exception();
 792 
 793   bool failures = false;
 794 
 795   for (int i = 0; i < objects->length(); i++) {
 796     assert(objects->at(i)->is_object(), "invalid debug information");
 797     ObjectValue* sv = (ObjectValue*) objects->at(i);
 798 
 799     Klass* k = java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()());
 800     oop obj = NULL;


1224       array->element(i)->free_monitors(thread);
1225 #ifdef ASSERT
1226       array->element(i)->set_removed_monitors();
1227 #endif
1228     }
1229   }
1230 }
1231 #endif
1232 
1233 static void collect_monitors(compiledVFrame* cvf, GrowableArray<Handle>* objects_to_revoke) {
1234   GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
1235   Thread* thread = Thread::current();
1236   for (int i = 0; i < monitors->length(); i++) {
1237     MonitorInfo* mon_info = monitors->at(i);
1238     if (!mon_info->eliminated() && mon_info->owner() != NULL) {
1239       objects_to_revoke->append(Handle(thread, mon_info->owner()));
1240     }
1241   }
1242 }
1243 
1244 
1245 void Deoptimization::revoke_biases_of_monitors(JavaThread* thread, frame fr, RegisterMap* map) {
1246   if (!UseBiasedLocking) {
1247     return;
1248   }
1249 
1250   GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>();
1251 
1252   // Unfortunately we don't have a RegisterMap available in most of
1253   // the places we want to call this routine so we need to walk the
1254   // stack again to update the register map.
1255   if (map == NULL || !map->update_map()) {
1256     StackFrameStream sfs(thread, true);
1257     bool found = false;
1258     while (!found && !sfs.is_done()) {
1259       frame* cur = sfs.current();
1260       sfs.next();
1261       found = cur->id() == fr.id();
1262     }
1263     assert(found, "frame to be deoptimized not found on target thread's stack");
1264     map = sfs.register_map();
1265   }
1266 
1267   vframe* vf = vframe::new_vframe(&fr, map, thread);
1268   compiledVFrame* cvf = compiledVFrame::cast(vf);
1269   // Revoke monitors' biases in all scopes
1270   while (!cvf->is_top()) {
1271     collect_monitors(cvf, objects_to_revoke);
1272     cvf = compiledVFrame::cast(cvf->sender());
1273   }
1274   collect_monitors(cvf, objects_to_revoke);








1275 
1276   if (SafepointSynchronize::is_at_safepoint()) {
1277     BiasedLocking::revoke_at_safepoint(objects_to_revoke);
1278   } else {
1279     BiasedLocking::revoke(objects_to_revoke);
1280   }
1281 }
1282 


















1283 
1284 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1285   assert(fr.can_be_deoptimized(), "checking frame type");
1286 
1287   gather_statistics(reason, Action_none, Bytecodes::_illegal);
1288 
1289   if (LogCompilation && xtty != NULL) {
1290     CompiledMethod* cm = fr.cb()->as_compiled_method_or_null();
1291     assert(cm != NULL, "only compiled methods can deopt");
1292 
1293     ttyLocker ttyl;
1294     xtty->begin_head("deoptimized thread='" UINTX_FORMAT "' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1295     cm->log_identity(xtty);
1296     xtty->end_head();
1297     for (ScopeDesc* sd = cm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1298       xtty->begin_elem("jvms bci='%d'", sd->bci());
1299       xtty->method(sd->method());
1300       xtty->end_elem();
1301       if (sd->is_top())  break;
1302     }
1303     xtty->tail("deoptimized");
1304   }
1305 
1306   // Patch the compiled method so that when execution returns to it we will
1307   // deopt the execution state and return to the interpreter.
1308   fr.deoptimize(thread);
1309 }
1310 
1311 void Deoptimization::deoptimize(JavaThread* thread, frame fr, RegisterMap *map) {
1312   deoptimize(thread, fr, map, Reason_constraint);
1313 }
1314 
1315 void Deoptimization::deoptimize(JavaThread* thread, frame fr, RegisterMap *map, DeoptReason reason) {





1316   // Deoptimize only if the frame comes from compile code.
1317   // Do not deoptimize the frame which is already patched
1318   // during the execution of the loops below.
1319   if (!fr.is_compiled_frame() || fr.is_deoptimized_frame()) {
1320     return;
1321   }
1322   ResourceMark rm;
1323   DeoptimizationMarker dm;
1324   if (UseBiasedLocking) {
1325     revoke_biases_of_monitors(thread, fr, map);




1326   }
1327   deoptimize_single_frame(thread, fr, reason);
1328 
1329 }
1330 
1331 #if INCLUDE_JVMCI
1332 address Deoptimization::deoptimize_for_missing_exception_handler(CompiledMethod* cm) {
1333   // there is no exception handler for this pc => deoptimize
1334   cm->make_not_entrant();
1335 
1336   // Use Deoptimization::deoptimize for all of its side-effects:
1337   // revoking biases of monitors, gathering traps statistics, logging...
1338   // it also patches the return pc but we do not care about that
1339   // since we return a continuation to the deopt_blob below.
1340   JavaThread* thread = JavaThread::current();
1341   RegisterMap reg_map(thread, UseBiasedLocking);
1342   frame runtime_frame = thread->last_frame();
1343   frame caller_frame = runtime_frame.sender(&reg_map);
1344   assert(caller_frame.cb()->as_compiled_method_or_null() == cm, "expect top frame compiled method");
1345   Deoptimization::deoptimize(thread, caller_frame, &reg_map, Deoptimization::Reason_not_compiled_exception_handler);


1470 #if INCLUDE_JVMCI
1471   // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
1472   RegisterMap reg_map(thread, true);
1473 #else
1474   RegisterMap reg_map(thread, UseBiasedLocking);
1475 #endif
1476   frame stub_frame = thread->last_frame();
1477   frame fr = stub_frame.sender(&reg_map);
1478   // Make sure the calling nmethod is not getting deoptimized and removed
1479   // before we are done with it.
1480   nmethodLocker nl(fr.pc());
1481 
1482   // Log a message
1483   Events::log(thread, "Uncommon trap: trap_request=" PTR32_FORMAT " fr.pc=" INTPTR_FORMAT " relative=" INTPTR_FORMAT,
1484               trap_request, p2i(fr.pc()), fr.pc() - fr.cb()->code_begin());
1485 
1486   {
1487     ResourceMark rm;
1488 
1489     // Revoke biases of any monitors in the frame to ensure we can migrate them
1490     revoke_biases_of_monitors(thread, fr, &reg_map);
1491 
1492     DeoptReason reason = trap_request_reason(trap_request);
1493     DeoptAction action = trap_request_action(trap_request);
1494 #if INCLUDE_JVMCI
1495     int debug_id = trap_request_debug_id(trap_request);
1496 #endif
1497     jint unloaded_class_index = trap_request_index(trap_request); // CP idx or -1
1498 
1499     vframe*  vf  = vframe::new_vframe(&fr, &reg_map, thread);
1500     compiledVFrame* cvf = compiledVFrame::cast(vf);
1501 
1502     CompiledMethod* nm = cvf->code();
1503 
1504     ScopeDesc*      trap_scope  = cvf->scope();
1505 
1506     if (TraceDeoptimization) {
1507       ttyLocker ttyl;
1508       tty->print_cr("  bci=%d pc=" INTPTR_FORMAT ", relative_pc=" INTPTR_FORMAT ", method=%s" JVMCI_ONLY(", debug_id=%d"), trap_scope->bci(), p2i(fr.pc()), fr.pc() - nm->code_begin(), trap_scope->method()->name_and_sig_as_C_string()
1509 #if INCLUDE_JVMCI
1510           , debug_id




 757             vframeArrayElement* el = cur_array->element(k);
 758             tty->print_cr("    %s (bci %d)", el->method()->name_and_sig_as_C_string(), el->bci());
 759           }
 760           cur_array->print_on_2(tty);
 761         } // release tty lock before calling guarantee
 762         guarantee(false, "wrong number of expression stack elements during deopt");
 763       }
 764       VerifyOopClosure verify;
 765       iframe->oops_interpreted_do(&verify, &rm, false);
 766       callee_size_of_parameters = mh->size_of_parameters();
 767       callee_max_locals = mh->max_locals();
 768       is_top_frame = false;
 769     }
 770   }
 771 #endif /* !PRODUCT */
 772 
 773 
 774   return bt;
 775 JRT_END
 776 
 777 class DeoptimizeMarkedTC : public ThreadClosure {
 778  bool _in_handshake;
 779  public:
 780   DeoptimizeMarkedTC(bool in_handshake) : _in_handshake(in_handshake) {}
 781   virtual void do_thread(Thread* thread) {
 782     assert(thread->is_Java_thread(), "must be");
 783     JavaThread* jt = (JavaThread*)thread;
 784     jt->deoptimize_marked_methods(_in_handshake);
 785   }
 786 };
 787 
 788 void Deoptimization::deoptimize_all_marked() {
 789   ResourceMark rm;
 790   DeoptimizationMarker dm;
 791 
 792   if (SafepointSynchronize::is_at_safepoint()) {
 793     DeoptimizeMarkedTC deopt(false);
 794     // Make the dependent methods not entrant
 795     CodeCache::make_marked_nmethods_not_entrant();
 796     Threads::java_threads_do(&deopt);
 797   } else {
 798     // Make the dependent methods not entrant
 799     {
 800       MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 801       CodeCache::make_marked_nmethods_not_entrant();
 802     }
 803     DeoptimizeMarkedTC deopt(true);
 804     Handshake::execute(&deopt);
 805   }
 806 }
 807 
 808 Deoptimization::DeoptAction Deoptimization::_unloaded_action
 809   = Deoptimization::Action_reinterpret;
 810 
 811 #if COMPILER2_OR_JVMCI
 812 bool Deoptimization::realloc_objects(JavaThread* thread, frame* fr, GrowableArray<ScopeValue*>* objects, TRAPS) {
 813   Handle pending_exception(THREAD, thread->pending_exception());
 814   const char* exception_file = thread->exception_file();
 815   int exception_line = thread->exception_line();
 816   thread->clear_pending_exception();
 817 
 818   bool failures = false;
 819 
 820   for (int i = 0; i < objects->length(); i++) {
 821     assert(objects->at(i)->is_object(), "invalid debug information");
 822     ObjectValue* sv = (ObjectValue*) objects->at(i);
 823 
 824     Klass* k = java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()());
 825     oop obj = NULL;


1249       array->element(i)->free_monitors(thread);
1250 #ifdef ASSERT
1251       array->element(i)->set_removed_monitors();
1252 #endif
1253     }
1254   }
1255 }
1256 #endif
1257 
1258 static void collect_monitors(compiledVFrame* cvf, GrowableArray<Handle>* objects_to_revoke) {
1259   GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
1260   Thread* thread = Thread::current();
1261   for (int i = 0; i < monitors->length(); i++) {
1262     MonitorInfo* mon_info = monitors->at(i);
1263     if (!mon_info->eliminated() && mon_info->owner() != NULL) {
1264       objects_to_revoke->append(Handle(thread, mon_info->owner()));
1265     }
1266   }
1267 }
1268 
1269 static void get_monitors_from_stack(GrowableArray<Handle>* objects_to_revoke, JavaThread* thread, frame fr, RegisterMap* map) {







1270   // Unfortunately we don't have a RegisterMap available in most of
1271   // the places we want to call this routine so we need to walk the
1272   // stack again to update the register map.
1273   if (map == NULL || !map->update_map()) {
1274     StackFrameStream sfs(thread, true);
1275     bool found = false;
1276     while (!found && !sfs.is_done()) {
1277       frame* cur = sfs.current();
1278       sfs.next();
1279       found = cur->id() == fr.id();
1280     }
1281     assert(found, "frame to be deoptimized not found on target thread's stack");
1282     map = sfs.register_map();
1283   }
1284 
1285   vframe* vf = vframe::new_vframe(&fr, map, thread);
1286   compiledVFrame* cvf = compiledVFrame::cast(vf);
1287   // Revoke monitors' biases in all scopes
1288   while (!cvf->is_top()) {
1289     collect_monitors(cvf, objects_to_revoke);
1290     cvf = compiledVFrame::cast(cvf->sender());
1291   }
1292   collect_monitors(cvf, objects_to_revoke);
1293 }
1294 
1295 void Deoptimization::inflate_monitors(JavaThread* thread, frame fr, RegisterMap* map) {
1296   if (!UseBiasedLocking) {
1297     return;
1298   }
1299   GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>();
1300   get_monitors_from_stack(objects_to_revoke, thread, fr, map);
1301 
1302   if (SafepointSynchronize::is_at_safepoint()) {
1303     BiasedLocking::revoke_at_safepoint(objects_to_revoke);
1304   } else {
1305     BiasedLocking::revoke(objects_to_revoke);
1306   }
1307 }
1308 
1309 void Deoptimization::inflate_monitors_handshake(JavaThread* thread, frame fr, RegisterMap* map) {
1310   if (!UseBiasedLocking) {
1311     return;
1312   }
1313   GrowableArray<Handle>* objects_to_revoke = new GrowableArray<Handle>();
1314   get_monitors_from_stack(objects_to_revoke, thread, fr, map);
1315 
1316   int len = objects_to_revoke->length();
1317   for (int i = 0; i < len; i++) {
1318     oop obj = (objects_to_revoke->at(i))();
1319     markOop mark = obj->mark();
1320     assert(!mark->has_bias_pattern() || mark->biased_locker() == thread, "Can't revoke");
1321     BiasedLocking::revoke_and_rebias_in_handshake(objects_to_revoke->at(i), thread);
1322     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
1323     ObjectSynchronizer::inflate(thread, obj, ObjectSynchronizer::inflate_cause_vm_internal);
1324   }
1325 }
1326 
1327 
1328 void Deoptimization::deoptimize_single_frame(JavaThread* thread, frame fr, Deoptimization::DeoptReason reason) {
1329   assert(fr.can_be_deoptimized(), "checking frame type");
1330 
1331   gather_statistics(reason, Action_none, Bytecodes::_illegal);
1332 
1333   if (LogCompilation && xtty != NULL) {
1334     CompiledMethod* cm = fr.cb()->as_compiled_method_or_null();
1335     assert(cm != NULL, "only compiled methods can deopt");
1336 
1337     ttyLocker ttyl;
1338     xtty->begin_head("deoptimized thread='" UINTX_FORMAT "' reason='%s' pc='" INTPTR_FORMAT "'",(uintx)thread->osthread()->thread_id(), trap_reason_name(reason), p2i(fr.pc()));
1339     cm->log_identity(xtty);
1340     xtty->end_head();
1341     for (ScopeDesc* sd = cm->scope_desc_at(fr.pc()); ; sd = sd->sender()) {
1342       xtty->begin_elem("jvms bci='%d'", sd->bci());
1343       xtty->method(sd->method());
1344       xtty->end_elem();
1345       if (sd->is_top())  break;
1346     }
1347     xtty->tail("deoptimized");
1348   }
1349 
1350   // Patch the compiled method so that when execution returns to it we will
1351   // deopt the execution state and return to the interpreter.
1352   fr.deoptimize(thread);
1353 }
1354 
1355 void Deoptimization::deoptimize(JavaThread* thread, frame fr, RegisterMap *map, bool in_handshake) {
1356   deopt_thread(in_handshake, thread, fr, map, Reason_constraint);
1357 }
1358 
1359 void Deoptimization::deoptimize(JavaThread* thread, frame fr, RegisterMap *map, DeoptReason reason) {
1360   deopt_thread(false, thread, fr, map, reason);
1361 }
1362 
1363 void Deoptimization::deopt_thread(bool in_handshake, JavaThread* thread,
1364                                   frame fr, RegisterMap *map, DeoptReason reason) {
1365   // Deoptimize only if the frame comes from compile code.
1366   // Do not deoptimize the frame which is already patched
1367   // during the execution of the loops below.
1368   if (!fr.is_compiled_frame() || fr.is_deoptimized_frame()) {
1369     return;
1370   }
1371   ResourceMark rm;
1372   DeoptimizationMarker dm;
1373   if (UseBiasedLocking) {
1374     if (in_handshake) {
1375       inflate_monitors_handshake(thread, fr, map);
1376     } else {
1377       inflate_monitors(thread, fr, map);
1378     }
1379   }
1380   deoptimize_single_frame(thread, fr, reason);
1381 
1382 }
1383 
1384 #if INCLUDE_JVMCI
1385 address Deoptimization::deoptimize_for_missing_exception_handler(CompiledMethod* cm) {
1386   // there is no exception handler for this pc => deoptimize
1387   cm->make_not_entrant();
1388 
1389   // Use Deoptimization::deoptimize for all of its side-effects:
1390   // revoking biases of monitors, gathering traps statistics, logging...
1391   // it also patches the return pc but we do not care about that
1392   // since we return a continuation to the deopt_blob below.
1393   JavaThread* thread = JavaThread::current();
1394   RegisterMap reg_map(thread, UseBiasedLocking);
1395   frame runtime_frame = thread->last_frame();
1396   frame caller_frame = runtime_frame.sender(&reg_map);
1397   assert(caller_frame.cb()->as_compiled_method_or_null() == cm, "expect top frame compiled method");
1398   Deoptimization::deoptimize(thread, caller_frame, &reg_map, Deoptimization::Reason_not_compiled_exception_handler);


1523 #if INCLUDE_JVMCI
1524   // JVMCI might need to get an exception from the stack, which in turn requires the register map to be valid
1525   RegisterMap reg_map(thread, true);
1526 #else
1527   RegisterMap reg_map(thread, UseBiasedLocking);
1528 #endif
1529   frame stub_frame = thread->last_frame();
1530   frame fr = stub_frame.sender(&reg_map);
1531   // Make sure the calling nmethod is not getting deoptimized and removed
1532   // before we are done with it.
1533   nmethodLocker nl(fr.pc());
1534 
1535   // Log a message
1536   Events::log(thread, "Uncommon trap: trap_request=" PTR32_FORMAT " fr.pc=" INTPTR_FORMAT " relative=" INTPTR_FORMAT,
1537               trap_request, p2i(fr.pc()), fr.pc() - fr.cb()->code_begin());
1538 
1539   {
1540     ResourceMark rm;
1541 
1542     // Revoke biases of any monitors in the frame to ensure we can migrate them
1543     fix_monitors(thread, fr, &reg_map);
1544 
1545     DeoptReason reason = trap_request_reason(trap_request);
1546     DeoptAction action = trap_request_action(trap_request);
1547 #if INCLUDE_JVMCI
1548     int debug_id = trap_request_debug_id(trap_request);
1549 #endif
1550     jint unloaded_class_index = trap_request_index(trap_request); // CP idx or -1
1551 
1552     vframe*  vf  = vframe::new_vframe(&fr, &reg_map, thread);
1553     compiledVFrame* cvf = compiledVFrame::cast(vf);
1554 
1555     CompiledMethod* nm = cvf->code();
1556 
1557     ScopeDesc*      trap_scope  = cvf->scope();
1558 
1559     if (TraceDeoptimization) {
1560       ttyLocker ttyl;
1561       tty->print_cr("  bci=%d pc=" INTPTR_FORMAT ", relative_pc=" INTPTR_FORMAT ", method=%s" JVMCI_ONLY(", debug_id=%d"), trap_scope->bci(), p2i(fr.pc()), fr.pc() - nm->code_begin(), trap_scope->method()->name_and_sig_as_C_string()
1562 #if INCLUDE_JVMCI
1563           , debug_id


< prev index next >