< prev index next >

src/hotspot/share/runtime/deoptimization.cpp

Print this page
rev 54936 : [mq]: 8221734-v3
rev 54937 : [mq]: 8221734-v5


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














 779 
 780 int Deoptimization::deoptimize_dependents() {
 781   Threads::deoptimized_wrt_marked_nmethods();
 782   return 0;











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


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








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















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





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




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




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


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







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


< prev index next >