hotspot/src/share/vm/oops/instanceKlass.cpp
changeset 40927 59f3c8a69541
parent 40909 0bb3d95b9577
child 41182 dbd59c1da636
equal deleted inserted replaced
40925:04b19236aa98 40927:59f3c8a69541
  3363 }
  3363 }
  3364 #endif
  3364 #endif
  3365 
  3365 
  3366 #if INCLUDE_JVMTI
  3366 #if INCLUDE_JVMTI
  3367 
  3367 
  3368 // RedefineClasses() support for previous versions:
  3368 // RedefineClasses() support for previous versions
  3369 int InstanceKlass::_previous_version_count = 0;
  3369 
  3370 
  3370 // Globally, there is at least one previous version of a class to walk
  3371 // Purge previous versions before adding new previous versions of the class.
  3371 // during class unloading, which is saved because old methods in the class
  3372 void InstanceKlass::purge_previous_versions(InstanceKlass* ik) {
  3372 // are still running.   Otherwise the previous version list is cleaned up.
  3373   if (ik->previous_versions() != NULL) {
  3373 bool InstanceKlass::_has_previous_versions = false;
  3374     // This klass has previous versions so see what we can cleanup
  3374 
  3375     // while it is safe to do so.
  3375 // Returns true if there are previous versions of a class for class
  3376 
  3376 // unloading only. Also resets the flag to false. purge_previous_version
  3377     int deleted_count = 0;    // leave debugging breadcrumbs
  3377 // will set the flag to true if there are any left, i.e., if there's any
  3378     int live_count = 0;
  3378 // work to do for next time. This is to avoid the expensive code cache
  3379     ClassLoaderData* loader_data = ik->class_loader_data();
  3379 // walk in CLDG::do_unloading().
  3380     assert(loader_data != NULL, "should never be null");
  3380 bool InstanceKlass::has_previous_versions_and_reset() {
  3381 
  3381   bool ret = _has_previous_versions;
  3382     ResourceMark rm;
  3382   log_trace(redefine, class, iklass, purge)("Class unloading: has_previous_versions = %s",
  3383     log_trace(redefine, class, iklass, purge)("%s: previous versions", ik->external_name());
  3383      ret ? "true" : "false");
  3384 
  3384   _has_previous_versions = false;
  3385     // previous versions are linked together through the InstanceKlass
  3385   return ret;
  3386     InstanceKlass* pv_node = ik->previous_versions();
  3386 }
  3387     InstanceKlass* last = ik;
  3387 
  3388     int version = 0;
  3388 // Purge previous versions before adding new previous versions of the class and
  3389 
  3389 // during class unloading.
  3390     // check the previous versions list
  3390 void InstanceKlass::purge_previous_version_list() {
  3391     for (; pv_node != NULL; ) {
  3391   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
  3392 
  3392   assert(has_been_redefined(), "Should only be called for main class");
  3393       ConstantPool* pvcp = pv_node->constants();
  3393 
  3394       assert(pvcp != NULL, "cp ref was unexpectedly cleared");
  3394   // Quick exit.
  3395 
  3395   if (previous_versions() == NULL) {
  3396       if (!pvcp->on_stack()) {
  3396     return;
  3397         // If the constant pool isn't on stack, none of the methods
  3397   }
  3398         // are executing.  Unlink this previous_version.
  3398 
  3399         // The previous version InstanceKlass is on the ClassLoaderData deallocate list
  3399   // This klass has previous versions so see what we can cleanup
  3400         // so will be deallocated during the next phase of class unloading.
  3400   // while it is safe to do so.
  3401         log_trace(redefine, class, iklass, purge)("previous version " INTPTR_FORMAT " is dead", p2i(pv_node));
  3401 
  3402         // For debugging purposes.
  3402   int deleted_count = 0;    // leave debugging breadcrumbs
  3403         pv_node->set_is_scratch_class();
  3403   int live_count = 0;
  3404         pv_node->class_loader_data()->add_to_deallocate_list(pv_node);
  3404   ClassLoaderData* loader_data = class_loader_data();
  3405         pv_node = pv_node->previous_versions();
  3405   assert(loader_data != NULL, "should never be null");
  3406         last->link_previous_versions(pv_node);
  3406 
  3407         deleted_count++;
  3407   ResourceMark rm;
  3408         version++;
  3408   log_trace(redefine, class, iklass, purge)("%s: previous versions", external_name());
  3409         continue;
  3409 
  3410       } else {
  3410   // previous versions are linked together through the InstanceKlass
  3411         log_trace(redefine, class, iklass, purge)("previous version " INTPTR_FORMAT " is alive", p2i(pv_node));
  3411   InstanceKlass* pv_node = previous_versions();
  3412         assert(pvcp->pool_holder() != NULL, "Constant pool with no holder");
  3412   InstanceKlass* last = this;
  3413         guarantee (!loader_data->is_unloading(), "unloaded classes can't be on the stack");
  3413   int version = 0;
  3414         live_count++;
  3414 
  3415       }
  3415   // check the previous versions list
  3416 
  3416   for (; pv_node != NULL; ) {
  3417       // At least one method is live in this previous version.
  3417 
  3418       // Reset dead EMCP methods not to get breakpoints.
  3418     ConstantPool* pvcp = pv_node->constants();
  3419       // All methods are deallocated when all of the methods for this class are no
  3419     assert(pvcp != NULL, "cp ref was unexpectedly cleared");
  3420       // longer running.
  3420 
  3421       Array<Method*>* method_refs = pv_node->methods();
  3421     if (!pvcp->on_stack()) {
  3422       if (method_refs != NULL) {
  3422       // If the constant pool isn't on stack, none of the methods
  3423         log_trace(redefine, class, iklass, purge)("previous methods length=%d", method_refs->length());
  3423       // are executing.  Unlink this previous_version.
  3424         for (int j = 0; j < method_refs->length(); j++) {
  3424       // The previous version InstanceKlass is on the ClassLoaderData deallocate list
  3425           Method* method = method_refs->at(j);
  3425       // so will be deallocated during the next phase of class unloading.
  3426 
  3426       log_trace(redefine, class, iklass, purge)
  3427           if (!method->on_stack()) {
  3427         ("previous version " INTPTR_FORMAT " is dead.", p2i(pv_node));
  3428             // no breakpoints for non-running methods
  3428       // For debugging purposes.
  3429             if (method->is_running_emcp()) {
  3429       pv_node->set_is_scratch_class();
  3430               method->set_running_emcp(false);
  3430       // Unlink from previous version list.
  3431             }
  3431       assert(pv_node->class_loader_data() == loader_data, "wrong loader_data");
  3432           } else {
  3432       InstanceKlass* next = pv_node->previous_versions();
  3433             assert (method->is_obsolete() || method->is_running_emcp(),
  3433       pv_node->link_previous_versions(NULL);   // point next to NULL
  3434                     "emcp method cannot run after emcp bit is cleared");
  3434       last->link_previous_versions(next);
  3435             log_trace(redefine, class, iklass, purge)
  3435       // Add to the deallocate list after unlinking
  3436               ("purge: %s(%s): prev method @%d in version @%d is alive",
  3436       loader_data->add_to_deallocate_list(pv_node);
  3437                method->name()->as_C_string(), method->signature()->as_C_string(), j, version);
  3437       pv_node = next;
       
  3438       deleted_count++;
       
  3439       version++;
       
  3440       continue;
       
  3441     } else {
       
  3442       log_trace(redefine, class, iklass, purge)("previous version " INTPTR_FORMAT " is alive", p2i(pv_node));
       
  3443       assert(pvcp->pool_holder() != NULL, "Constant pool with no holder");
       
  3444       guarantee (!loader_data->is_unloading(), "unloaded classes can't be on the stack");
       
  3445       live_count++;
       
  3446       // found a previous version for next time we do class unloading
       
  3447       _has_previous_versions = true;
       
  3448     }
       
  3449 
       
  3450     // At least one method is live in this previous version.
       
  3451     // Reset dead EMCP methods not to get breakpoints.
       
  3452     // All methods are deallocated when all of the methods for this class are no
       
  3453     // longer running.
       
  3454     Array<Method*>* method_refs = pv_node->methods();
       
  3455     if (method_refs != NULL) {
       
  3456       log_trace(redefine, class, iklass, purge)("previous methods length=%d", method_refs->length());
       
  3457       for (int j = 0; j < method_refs->length(); j++) {
       
  3458         Method* method = method_refs->at(j);
       
  3459 
       
  3460         if (!method->on_stack()) {
       
  3461           // no breakpoints for non-running methods
       
  3462           if (method->is_running_emcp()) {
       
  3463             method->set_running_emcp(false);
  3438           }
  3464           }
       
  3465         } else {
       
  3466           assert (method->is_obsolete() || method->is_running_emcp(),
       
  3467                   "emcp method cannot run after emcp bit is cleared");
       
  3468           log_trace(redefine, class, iklass, purge)
       
  3469             ("purge: %s(%s): prev method @%d in version @%d is alive",
       
  3470              method->name()->as_C_string(), method->signature()->as_C_string(), j, version);
  3439         }
  3471         }
  3440       }
  3472       }
  3441       // next previous version
  3473     }
  3442       last = pv_node;
  3474     // next previous version
  3443       pv_node = pv_node->previous_versions();
  3475     last = pv_node;
  3444       version++;
  3476     pv_node = pv_node->previous_versions();
  3445     }
  3477     version++;
  3446     log_trace(redefine, class, iklass, purge)
  3478   }
  3447       ("previous version stats: live=%d, deleted=%d",
  3479   log_trace(redefine, class, iklass, purge)
  3448        live_count, deleted_count);
  3480     ("previous version stats: live=%d, deleted=%d", live_count, deleted_count);
  3449   }
       
  3450 }
  3481 }
  3451 
  3482 
  3452 void InstanceKlass::mark_newly_obsolete_methods(Array<Method*>* old_methods,
  3483 void InstanceKlass::mark_newly_obsolete_methods(Array<Method*>* old_methods,
  3453                                                 int emcp_method_count) {
  3484                                                 int emcp_method_count) {
  3454   int obsolete_method_count = old_methods->length() - emcp_method_count;
  3485   int obsolete_method_count = old_methods->length() - emcp_method_count;
  3516 
  3547 
  3517   ResourceMark rm;
  3548   ResourceMark rm;
  3518   log_trace(redefine, class, iklass, add)
  3549   log_trace(redefine, class, iklass, add)
  3519     ("adding previous version ref for %s, EMCP_cnt=%d", scratch_class->external_name(), emcp_method_count);
  3550     ("adding previous version ref for %s, EMCP_cnt=%d", scratch_class->external_name(), emcp_method_count);
  3520 
  3551 
  3521   // Clean out old previous versions
  3552   // Clean out old previous versions for this class
  3522   purge_previous_versions(this);
  3553   purge_previous_version_list();
  3523 
  3554 
  3524   // Mark newly obsolete methods in remaining previous versions.  An EMCP method from
  3555   // Mark newly obsolete methods in remaining previous versions.  An EMCP method from
  3525   // a previous redefinition may be made obsolete by this redefinition.
  3556   // a previous redefinition may be made obsolete by this redefinition.
  3526   Array<Method*>* old_methods = scratch_class->methods();
  3557   Array<Method*>* old_methods = scratch_class->methods();
  3527   mark_newly_obsolete_methods(old_methods, emcp_method_count);
  3558   mark_newly_obsolete_methods(old_methods, emcp_method_count);
  3534   if (!cp_ref->on_stack()) {
  3565   if (!cp_ref->on_stack()) {
  3535     log_trace(redefine, class, iklass, add)("scratch class not added; no methods are running");
  3566     log_trace(redefine, class, iklass, add)("scratch class not added; no methods are running");
  3536     // For debugging purposes.
  3567     // For debugging purposes.
  3537     scratch_class->set_is_scratch_class();
  3568     scratch_class->set_is_scratch_class();
  3538     scratch_class->class_loader_data()->add_to_deallocate_list(scratch_class());
  3569     scratch_class->class_loader_data()->add_to_deallocate_list(scratch_class());
  3539     // Update count for class unloading.
       
  3540     _previous_version_count--;
       
  3541     return;
  3570     return;
  3542   }
  3571   }
  3543 
  3572 
  3544   if (emcp_method_count != 0) {
  3573   if (emcp_method_count != 0) {
  3545     // At least one method is still running, check for EMCP methods
  3574     // At least one method is still running, check for EMCP methods
  3563       }
  3592       }
  3564     }
  3593     }
  3565   }
  3594   }
  3566 
  3595 
  3567   // Add previous version if any methods are still running.
  3596   // Add previous version if any methods are still running.
  3568   log_trace(redefine, class, iklass, add)("scratch class added; one of its methods is on_stack");
  3597   // Set has_previous_version flag for processing during class unloading.
       
  3598   _has_previous_versions = true;
       
  3599   log_trace(redefine, class, iklass, add) ("scratch class added; one of its methods is on_stack.");
  3569   assert(scratch_class->previous_versions() == NULL, "shouldn't have a previous version");
  3600   assert(scratch_class->previous_versions() == NULL, "shouldn't have a previous version");
  3570   scratch_class->link_previous_versions(previous_versions());
  3601   scratch_class->link_previous_versions(previous_versions());
  3571   link_previous_versions(scratch_class());
  3602   link_previous_versions(scratch_class());
  3572   // Update count for class unloading.
       
  3573   _previous_version_count++;
       
  3574 } // end add_previous_version()
  3603 } // end add_previous_version()
  3575 
  3604 
  3576 #endif // INCLUDE_JVMTI
  3605 #endif // INCLUDE_JVMTI
  3577 
  3606 
  3578 Method* InstanceKlass::method_with_idnum(int idnum) {
  3607 Method* InstanceKlass::method_with_idnum(int idnum) {