hotspot/src/share/vm/runtime/deoptimization.cpp
changeset 28039 bf5a8340bf8a
parent 27880 afb974a04396
child 28047 c0e7a15c458f
equal deleted inserted replaced
28038:76e7ca4c8b6e 28039:bf5a8340bf8a
   174     vf = vf->sender();
   174     vf = vf->sender();
   175   }
   175   }
   176   assert(vf->is_compiled_frame(), "Wrong frame type");
   176   assert(vf->is_compiled_frame(), "Wrong frame type");
   177   chunk->push(compiledVFrame::cast(vf));
   177   chunk->push(compiledVFrame::cast(vf));
   178 
   178 
       
   179   bool realloc_failures = false;
       
   180 
   179 #ifdef COMPILER2
   181 #ifdef COMPILER2
   180   // Reallocate the non-escaping objects and restore their fields. Then
   182   // Reallocate the non-escaping objects and restore their fields. Then
   181   // relock objects if synchronization on them was eliminated.
   183   // relock objects if synchronization on them was eliminated.
   182   if (DoEscapeAnalysis || EliminateNestedLocks) {
   184   if (DoEscapeAnalysis || EliminateNestedLocks) {
   183     if (EliminateAllocations) {
   185     if (EliminateAllocations) {
   204         if (TraceDeoptimization) {
   206         if (TraceDeoptimization) {
   205           ttyLocker ttyl;
   207           ttyLocker ttyl;
   206           tty->print_cr("SAVED OOP RESULT " INTPTR_FORMAT " in thread " INTPTR_FORMAT, (void *)result, thread);
   208           tty->print_cr("SAVED OOP RESULT " INTPTR_FORMAT " in thread " INTPTR_FORMAT, (void *)result, thread);
   207         }
   209         }
   208       }
   210       }
   209       bool reallocated = false;
       
   210       if (objects != NULL) {
   211       if (objects != NULL) {
   211         JRT_BLOCK
   212         JRT_BLOCK
   212           reallocated = realloc_objects(thread, &deoptee, objects, THREAD);
   213           realloc_failures = realloc_objects(thread, &deoptee, objects, THREAD);
   213         JRT_END
   214         JRT_END
   214       }
   215         reassign_fields(&deoptee, &map, objects, realloc_failures);
   215       if (reallocated) {
   216       }
   216         reassign_fields(&deoptee, &map, objects);
       
   217 #ifndef PRODUCT
   217 #ifndef PRODUCT
   218         if (TraceDeoptimization) {
   218       if (TraceDeoptimization) {
   219           ttyLocker ttyl;
   219         ttyLocker ttyl;
   220           tty->print_cr("REALLOC OBJECTS in thread " INTPTR_FORMAT, thread);
   220         tty->print_cr("REALLOC OBJECTS in thread " INTPTR_FORMAT, thread);
   221           print_objects(objects);
   221         print_objects(objects, realloc_failures);
   222         }
   222       }
   223 #endif
   223 #endif
   224       }
       
   225       if (save_oop_result) {
   224       if (save_oop_result) {
   226         // Restore result.
   225         // Restore result.
   227         deoptee.set_saved_oop_result(&map, return_value());
   226         deoptee.set_saved_oop_result(&map, return_value());
   228       }
   227       }
   229     }
   228     }
   234       for (int i = 0; i < chunk->length(); i++) {
   233       for (int i = 0; i < chunk->length(); i++) {
   235         compiledVFrame* cvf = chunk->at(i);
   234         compiledVFrame* cvf = chunk->at(i);
   236         assert (cvf->scope() != NULL,"expect only compiled java frames");
   235         assert (cvf->scope() != NULL,"expect only compiled java frames");
   237         GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
   236         GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
   238         if (monitors->is_nonempty()) {
   237         if (monitors->is_nonempty()) {
   239           relock_objects(monitors, thread);
   238           relock_objects(monitors, thread, realloc_failures);
   240 #ifndef PRODUCT
   239 #ifndef PRODUCT
   241           if (TraceDeoptimization) {
   240           if (TraceDeoptimization) {
   242             ttyLocker ttyl;
   241             ttyLocker ttyl;
   243             for (int j = 0; j < monitors->length(); j++) {
   242             for (int j = 0; j < monitors->length(); j++) {
   244               MonitorInfo* mi = monitors->at(j);
   243               MonitorInfo* mi = monitors->at(j);
   245               if (mi->eliminated()) {
   244               if (mi->eliminated()) {
   246                 if (first) {
   245                 if (first) {
   247                   first = false;
   246                   first = false;
   248                   tty->print_cr("RELOCK OBJECTS in thread " INTPTR_FORMAT, thread);
   247                   tty->print_cr("RELOCK OBJECTS in thread " INTPTR_FORMAT, thread);
   249                 }
   248                 }
   250                 tty->print_cr("     object <" INTPTR_FORMAT "> locked", (void *)mi->owner());
   249                 if (mi->owner_is_scalar_replaced()) {
       
   250                   Klass* k = java_lang_Class::as_Klass(mi->owner_klass());
       
   251                   tty->print_cr("     failed reallocation for klass %s", k->external_name());
       
   252                 } else {
       
   253                   tty->print_cr("     object <" INTPTR_FORMAT "> locked", (void *)mi->owner());
       
   254                 }
   251               }
   255               }
   252             }
   256             }
   253           }
   257           }
   254 #endif
   258 #endif
   255         }
   259         }
   260   // Ensure that no safepoint is taken after pointers have been stored
   264   // Ensure that no safepoint is taken after pointers have been stored
   261   // in fields of rematerialized objects.  If a safepoint occurs from here on
   265   // in fields of rematerialized objects.  If a safepoint occurs from here on
   262   // out the java state residing in the vframeArray will be missed.
   266   // out the java state residing in the vframeArray will be missed.
   263   No_Safepoint_Verifier no_safepoint;
   267   No_Safepoint_Verifier no_safepoint;
   264 
   268 
   265   vframeArray* array = create_vframeArray(thread, deoptee, &map, chunk);
   269   vframeArray* array = create_vframeArray(thread, deoptee, &map, chunk, realloc_failures);
   266 
   270 #ifdef COMPILER2
   267   assert(thread->vframe_array_head() == NULL, "Pending deopt!");;
   271   if (realloc_failures) {
       
   272     pop_frames_failed_reallocs(thread, array);
       
   273   }
       
   274 #endif
       
   275 
       
   276   assert(thread->vframe_array_head() == NULL, "Pending deopt!");
   268   thread->set_vframe_array_head(array);
   277   thread->set_vframe_array_head(array);
   269 
   278 
   270   // Now that the vframeArray has been created if we have any deferred local writes
   279   // Now that the vframeArray has been created if we have any deferred local writes
   271   // added by jvmti then we can free up that structure as the data is now in the
   280   // added by jvmti then we can free up that structure as the data is now in the
   272   // vframeArray
   281   // vframeArray
   716   Handle pending_exception(thread->pending_exception());
   725   Handle pending_exception(thread->pending_exception());
   717   const char* exception_file = thread->exception_file();
   726   const char* exception_file = thread->exception_file();
   718   int exception_line = thread->exception_line();
   727   int exception_line = thread->exception_line();
   719   thread->clear_pending_exception();
   728   thread->clear_pending_exception();
   720 
   729 
       
   730   bool failures = false;
       
   731 
   721   for (int i = 0; i < objects->length(); i++) {
   732   for (int i = 0; i < objects->length(); i++) {
   722     assert(objects->at(i)->is_object(), "invalid debug information");
   733     assert(objects->at(i)->is_object(), "invalid debug information");
   723     ObjectValue* sv = (ObjectValue*) objects->at(i);
   734     ObjectValue* sv = (ObjectValue*) objects->at(i);
   724 
   735 
   725     KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()()));
   736     KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()()));
   726     oop obj = NULL;
   737     oop obj = NULL;
   727 
   738 
   728     if (k->oop_is_instance()) {
   739     if (k->oop_is_instance()) {
   729       InstanceKlass* ik = InstanceKlass::cast(k());
   740       InstanceKlass* ik = InstanceKlass::cast(k());
   730       obj = ik->allocate_instance(CHECK_(false));
   741       obj = ik->allocate_instance(THREAD);
   731     } else if (k->oop_is_typeArray()) {
   742     } else if (k->oop_is_typeArray()) {
   732       TypeArrayKlass* ak = TypeArrayKlass::cast(k());
   743       TypeArrayKlass* ak = TypeArrayKlass::cast(k());
   733       assert(sv->field_size() % type2size[ak->element_type()] == 0, "non-integral array length");
   744       assert(sv->field_size() % type2size[ak->element_type()] == 0, "non-integral array length");
   734       int len = sv->field_size() / type2size[ak->element_type()];
   745       int len = sv->field_size() / type2size[ak->element_type()];
   735       obj = ak->allocate(len, CHECK_(false));
   746       obj = ak->allocate(len, THREAD);
   736     } else if (k->oop_is_objArray()) {
   747     } else if (k->oop_is_objArray()) {
   737       ObjArrayKlass* ak = ObjArrayKlass::cast(k());
   748       ObjArrayKlass* ak = ObjArrayKlass::cast(k());
   738       obj = ak->allocate(sv->field_size(), CHECK_(false));
   749       obj = ak->allocate(sv->field_size(), THREAD);
   739     }
   750     }
   740 
   751 
   741     assert(obj != NULL, "allocation failed");
   752     if (obj == NULL) {
       
   753       failures = true;
       
   754     }
       
   755 
   742     assert(sv->value().is_null(), "redundant reallocation");
   756     assert(sv->value().is_null(), "redundant reallocation");
       
   757     assert(obj != NULL || HAS_PENDING_EXCEPTION, "allocation should succeed or we should get an exception");
       
   758     CLEAR_PENDING_EXCEPTION;
   743     sv->set_value(obj);
   759     sv->set_value(obj);
   744   }
   760   }
   745 
   761 
   746   if (pending_exception.not_null()) {
   762   if (failures) {
       
   763     THROW_OOP_(Universe::out_of_memory_error_realloc_objects(), failures);
       
   764   } else if (pending_exception.not_null()) {
   747     thread->set_pending_exception(pending_exception(), exception_file, exception_line);
   765     thread->set_pending_exception(pending_exception(), exception_file, exception_line);
   748   }
   766   }
   749 
   767 
   750   return true;
   768   return failures;
   751 }
   769 }
   752 
   770 
   753 // This assumes that the fields are stored in ObjectValue in the same order
   771 // This assumes that the fields are stored in ObjectValue in the same order
   754 // they are yielded by do_nonstatic_fields.
   772 // they are yielded by do_nonstatic_fields.
   755 class FieldReassigner: public FieldClosure {
   773 class FieldReassigner: public FieldClosure {
   883   }
   901   }
   884 }
   902 }
   885 
   903 
   886 
   904 
   887 // restore fields of all eliminated objects and arrays
   905 // restore fields of all eliminated objects and arrays
   888 void Deoptimization::reassign_fields(frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects) {
   906 void Deoptimization::reassign_fields(frame* fr, RegisterMap* reg_map, GrowableArray<ScopeValue*>* objects, bool realloc_failures) {
   889   for (int i = 0; i < objects->length(); i++) {
   907   for (int i = 0; i < objects->length(); i++) {
   890     ObjectValue* sv = (ObjectValue*) objects->at(i);
   908     ObjectValue* sv = (ObjectValue*) objects->at(i);
   891     KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()()));
   909     KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()()));
   892     Handle obj = sv->value();
   910     Handle obj = sv->value();
   893     assert(obj.not_null(), "reallocation was missed");
   911     assert(obj.not_null() || realloc_failures, "reallocation was missed");
       
   912     if (obj.is_null()) {
       
   913       continue;
       
   914     }
   894 
   915 
   895     if (k->oop_is_instance()) {
   916     if (k->oop_is_instance()) {
   896       InstanceKlass* ik = InstanceKlass::cast(k());
   917       InstanceKlass* ik = InstanceKlass::cast(k());
   897       FieldReassigner reassign(fr, reg_map, sv, obj());
   918       FieldReassigner reassign(fr, reg_map, sv, obj());
   898       ik->do_nonstatic_fields(&reassign);
   919       ik->do_nonstatic_fields(&reassign);
   905   }
   926   }
   906 }
   927 }
   907 
   928 
   908 
   929 
   909 // relock objects for which synchronization was eliminated
   930 // relock objects for which synchronization was eliminated
   910 void Deoptimization::relock_objects(GrowableArray<MonitorInfo*>* monitors, JavaThread* thread) {
   931 void Deoptimization::relock_objects(GrowableArray<MonitorInfo*>* monitors, JavaThread* thread, bool realloc_failures) {
   911   for (int i = 0; i < monitors->length(); i++) {
   932   for (int i = 0; i < monitors->length(); i++) {
   912     MonitorInfo* mon_info = monitors->at(i);
   933     MonitorInfo* mon_info = monitors->at(i);
   913     if (mon_info->eliminated()) {
   934     if (mon_info->eliminated()) {
   914       assert(mon_info->owner() != NULL, "reallocation was missed");
   935       assert(!mon_info->owner_is_scalar_replaced() || realloc_failures, "reallocation was missed");
   915       Handle obj = Handle(mon_info->owner());
   936       if (!mon_info->owner_is_scalar_replaced()) {
   916       markOop mark = obj->mark();
   937         Handle obj = Handle(mon_info->owner());
   917       if (UseBiasedLocking && mark->has_bias_pattern()) {
   938         markOop mark = obj->mark();
   918         // New allocated objects may have the mark set to anonymously biased.
   939         if (UseBiasedLocking && mark->has_bias_pattern()) {
   919         // Also the deoptimized method may called methods with synchronization
   940           // New allocated objects may have the mark set to anonymously biased.
   920         // where the thread-local object is bias locked to the current thread.
   941           // Also the deoptimized method may called methods with synchronization
   921         assert(mark->is_biased_anonymously() ||
   942           // where the thread-local object is bias locked to the current thread.
   922                mark->biased_locker() == thread, "should be locked to current thread");
   943           assert(mark->is_biased_anonymously() ||
   923         // Reset mark word to unbiased prototype.
   944                  mark->biased_locker() == thread, "should be locked to current thread");
   924         markOop unbiased_prototype = markOopDesc::prototype()->set_age(mark->age());
   945           // Reset mark word to unbiased prototype.
   925         obj->set_mark(unbiased_prototype);
   946           markOop unbiased_prototype = markOopDesc::prototype()->set_age(mark->age());
   926       }
   947           obj->set_mark(unbiased_prototype);
   927       BasicLock* lock = mon_info->lock();
   948         }
   928       ObjectSynchronizer::slow_enter(obj, lock, thread);
   949         BasicLock* lock = mon_info->lock();
   929     }
   950         ObjectSynchronizer::slow_enter(obj, lock, thread);
   930     assert(mon_info->owner()->is_locked(), "object must be locked now");
   951         assert(mon_info->owner()->is_locked(), "object must be locked now");
       
   952       }
       
   953     }
   931   }
   954   }
   932 }
   955 }
   933 
   956 
   934 
   957 
   935 #ifndef PRODUCT
   958 #ifndef PRODUCT
   936 // print information about reallocated objects
   959 // print information about reallocated objects
   937 void Deoptimization::print_objects(GrowableArray<ScopeValue*>* objects) {
   960 void Deoptimization::print_objects(GrowableArray<ScopeValue*>* objects, bool realloc_failures) {
   938   fieldDescriptor fd;
   961   fieldDescriptor fd;
   939 
   962 
   940   for (int i = 0; i < objects->length(); i++) {
   963   for (int i = 0; i < objects->length(); i++) {
   941     ObjectValue* sv = (ObjectValue*) objects->at(i);
   964     ObjectValue* sv = (ObjectValue*) objects->at(i);
   942     KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()()));
   965     KlassHandle k(java_lang_Class::as_Klass(sv->klass()->as_ConstantOopReadValue()->value()()));
   943     Handle obj = sv->value();
   966     Handle obj = sv->value();
   944 
   967 
   945     tty->print("     object <" INTPTR_FORMAT "> of type ", (void *)sv->value()());
   968     tty->print("     object <" INTPTR_FORMAT "> of type ", (void *)sv->value()());
   946     k->print_value();
   969     k->print_value();
   947     tty->print(" allocated (%d bytes)", obj->size() * HeapWordSize);
   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     }
   948     tty->cr();
   976     tty->cr();
   949 
   977 
   950     if (Verbose) {
   978     if (Verbose && !obj.is_null()) {
   951       k->oop_print_on(obj(), tty);
   979       k->oop_print_on(obj(), tty);
   952     }
   980     }
   953   }
   981   }
   954 }
   982 }
   955 #endif
   983 #endif
   956 #endif // COMPILER2
   984 #endif // COMPILER2
   957 
   985 
   958 vframeArray* Deoptimization::create_vframeArray(JavaThread* thread, frame fr, RegisterMap *reg_map, GrowableArray<compiledVFrame*>* chunk) {
   986 vframeArray* Deoptimization::create_vframeArray(JavaThread* thread, frame fr, RegisterMap *reg_map, GrowableArray<compiledVFrame*>* chunk, bool realloc_failures) {
   959   Events::log(thread, "DEOPT PACKING pc=" INTPTR_FORMAT " sp=" INTPTR_FORMAT, fr.pc(), fr.sp());
   987   Events::log(thread, "DEOPT PACKING pc=" INTPTR_FORMAT " sp=" INTPTR_FORMAT, fr.pc(), fr.sp());
   960 
   988 
   961 #ifndef PRODUCT
   989 #ifndef PRODUCT
   962   if (TraceDeoptimization) {
   990   if (TraceDeoptimization) {
   963     ttyLocker ttyl;
   991     ttyLocker ttyl;
   996   frame sender = caller;
  1024   frame sender = caller;
   997 
  1025 
   998   // Since the Java thread being deoptimized will eventually adjust it's own stack,
  1026   // Since the Java thread being deoptimized will eventually adjust it's own stack,
   999   // the vframeArray containing the unpacking information is allocated in the C heap.
  1027   // the vframeArray containing the unpacking information is allocated in the C heap.
  1000   // For Compiler1, the caller of the deoptimized frame is saved for use by unpack_frames().
  1028   // For Compiler1, the caller of the deoptimized frame is saved for use by unpack_frames().
  1001   vframeArray* array = vframeArray::allocate(thread, frame_size, chunk, reg_map, sender, caller, fr);
  1029   vframeArray* array = vframeArray::allocate(thread, frame_size, chunk, reg_map, sender, caller, fr, realloc_failures);
  1002 
  1030 
  1003   // Compare the vframeArray to the collected vframes
  1031   // Compare the vframeArray to the collected vframes
  1004   assert(array->structural_compare(thread, chunk), "just checking");
  1032   assert(array->structural_compare(thread, chunk), "just checking");
  1005 
  1033 
  1006 #ifndef PRODUCT
  1034 #ifndef PRODUCT
  1011 #endif // PRODUCT
  1039 #endif // PRODUCT
  1012 
  1040 
  1013   return array;
  1041   return array;
  1014 }
  1042 }
  1015 
  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++) {
       
  1058         BasicObjectLock* src = monitors->at(j);
       
  1059         if (src->obj() != NULL) {
       
  1060           ObjectSynchronizer::fast_exit(src->obj(), src->lock(), thread);
       
  1061         }
       
  1062       }
       
  1063       array->element(i)->free_monitors(thread);
       
  1064 #ifdef ASSERT
       
  1065       array->element(i)->set_removed_monitors();
       
  1066 #endif
       
  1067     }
       
  1068   }
       
  1069 }
       
  1070 #endif
  1016 
  1071 
  1017 static void collect_monitors(compiledVFrame* cvf, GrowableArray<Handle>* objects_to_revoke) {
  1072 static void collect_monitors(compiledVFrame* cvf, GrowableArray<Handle>* objects_to_revoke) {
  1018   GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
  1073   GrowableArray<MonitorInfo*>* monitors = cvf->monitors();
  1019   for (int i = 0; i < monitors->length(); i++) {
  1074   for (int i = 0; i < monitors->length(); i++) {
  1020     MonitorInfo* mon_info = monitors->at(i);
  1075     MonitorInfo* mon_info = monitors->at(i);