< prev index next >

src/share/vm/classfile/classLoaderData.cpp

Print this page




  47 // the singleton class the_null_class_loader_data().
  48 
  49 #include "precompiled.hpp"
  50 #include "classfile/classLoaderData.hpp"
  51 #include "classfile/classLoaderData.inline.hpp"
  52 #include "classfile/javaClasses.hpp"
  53 #include "classfile/metadataOnStackMark.hpp"
  54 #include "classfile/systemDictionary.hpp"
  55 #include "code/codeCache.hpp"
  56 #include "memory/gcLocker.hpp"
  57 #include "memory/metadataFactory.hpp"
  58 #include "memory/metaspaceShared.hpp"
  59 #include "memory/oopFactory.hpp"
  60 #include "runtime/jniHandles.hpp"
  61 #include "runtime/mutex.hpp"
  62 #include "runtime/safepoint.hpp"
  63 #include "runtime/synchronizer.hpp"
  64 #include "utilities/growableArray.hpp"
  65 #include "utilities/macros.hpp"
  66 #include "utilities/ostream.hpp"
  67 #if INCLUDE_TRACE
  68 #include "trace/tracing.hpp"
  69 #endif
  70 
  71 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
  72 
  73 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) :
  74   _class_loader(h_class_loader()),
  75   _is_anonymous(is_anonymous),
  76   // An anonymous class loader data doesn't have anything to keep
  77   // it from being unloaded during parsing of the anonymous class.
  78   // The null-class-loader should always be kept alive.
  79   _keep_alive(is_anonymous || h_class_loader.is_null()),
  80   _metaspace(NULL), _unloading(false), _klasses(NULL),
  81   _claimed(0), _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),
  82   _next(NULL), _dependencies(dependencies),
  83   _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {
  84     // empty

  85 }
  86 
  87 void ClassLoaderData::init_dependencies(TRAPS) {
  88   assert(!Universe::is_fully_initialized(), "should only be called when initializing");
  89   assert(is_the_null_class_loader_data(), "should only call this for the null class loader");
  90   _dependencies.init(CHECK);
  91 }
  92 
  93 void ClassLoaderData::Dependencies::init(TRAPS) {
  94   // Create empty dependencies array to add to. CMS requires this to be
  95   // an oop so that it can track additions via card marks.  We think.
  96   _list_head = oopFactory::new_objectArray(2, CHECK);
  97 }
  98 
  99 ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() {
 100   Chunk* c = _head;
 101   while (c != NULL) {
 102     Chunk* next = c->_next;
 103     delete c;
 104     c = next;


 629     if (cld->keep_alive()) {
 630       cld->oops_do(f, klass_closure, must_claim);
 631     }
 632   }
 633 }
 634 
 635 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
 636   if (ClassUnloading) {
 637     keep_alive_oops_do(f, klass_closure, must_claim);
 638   } else {
 639     oops_do(f, klass_closure, must_claim);
 640   }
 641 }
 642 
 643 void ClassLoaderDataGraph::cld_do(CLDClosure* cl) {
 644   for (ClassLoaderData* cld = _head; cl != NULL && cld != NULL; cld = cld->next()) {
 645     cl->do_cld(cld);
 646   }
 647 }
 648 










 649 void ClassLoaderDataGraph::roots_cld_do(CLDClosure* strong, CLDClosure* weak) {
 650   for (ClassLoaderData* cld = _head;  cld != NULL; cld = cld->_next) {
 651     CLDClosure* closure = cld->keep_alive() ? strong : weak;
 652     if (closure != NULL) {
 653       closure->do_cld(cld);
 654     }
 655   }
 656 }
 657 
 658 void ClassLoaderDataGraph::keep_alive_cld_do(CLDClosure* cl) {
 659   roots_cld_do(cl, NULL);
 660 }
 661 
 662 void ClassLoaderDataGraph::always_strong_cld_do(CLDClosure* cl) {
 663   if (ClassUnloading) {
 664     keep_alive_cld_do(cl);
 665   } else {
 666     cld_do(cl);
 667   }
 668 }


 723   for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) {
 724     if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {
 725       return true;
 726     }
 727   }
 728   return false;
 729 }
 730 
 731 #ifndef PRODUCT
 732 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {
 733   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
 734     if (loader_data == data) {
 735       return true;
 736     }
 737   }
 738 
 739   return false;
 740 }
 741 #endif // PRODUCT
 742 
 743 
 744 // Move class loader data from main list to the unloaded list for unloading
 745 // and deallocation later.
 746 bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive_closure, bool clean_alive) {
 747   ClassLoaderData* data = _head;
 748   ClassLoaderData* prev = NULL;
 749   bool seen_dead_loader = false;
 750 
 751   // Save previous _unloading pointer for CMS which may add to unloading list before
 752   // purging and we don't want to rewalk the previously unloaded class loader data.
 753   _saved_unloading = _unloading;
 754 
 755   while (data != NULL) {
 756     if (data->is_alive(is_alive_closure)) {
 757       prev = data;
 758       data = data->next();
 759       continue;
 760     }
 761     seen_dead_loader = true;
 762     ClassLoaderData* dead = data;
 763     dead->unload();
 764     data = data->next();
 765     // Remove from loader list.
 766     // This class loader data will no longer be found
 767     // in the ClassLoaderDataGraph.
 768     if (prev != NULL) {
 769       prev->set_next(data);
 770     } else {
 771       assert(dead == _head, "sanity check");
 772       _head = data;
 773     }
 774     dead->set_next(_unloading);
 775     _unloading = dead;
 776   }
 777 
 778   if (clean_alive) {
 779     // Clean previous versions and the deallocate list.
 780     ClassLoaderDataGraph::clean_metaspaces();
 781   }
 782 
 783   if (seen_dead_loader) {
 784     post_class_unload_events();
 785   }
 786 
 787   return seen_dead_loader;
 788 }
 789 
 790 void ClassLoaderDataGraph::clean_metaspaces() {
 791   // mark metadata seen on the stack and code cache so we can delete unneeded entries.
 792   bool has_redefined_a_class = JvmtiExport::has_redefined_a_class();
 793   MetadataOnStackMark md_on_stack(has_redefined_a_class);
 794 
 795   if (has_redefined_a_class) {
 796     // purge_previous_versions also cleans weak method links. Because
 797     // one method's MDO can reference another method from another
 798     // class loader, we need to first clean weak method links for all
 799     // class loaders here. Below, we can then free redefined methods
 800     // for all class loaders.
 801     for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
 802       data->classes_do(InstanceKlass::purge_previous_versions);
 803     }
 804   }
 805 
 806   // Need to purge the previous version before deallocating.
 807   free_deallocate_lists();
 808 }
 809 
 810 void ClassLoaderDataGraph::purge() {
 811   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 812   ClassLoaderData* list = _unloading;
 813   _unloading = NULL;
 814   ClassLoaderData* next = list;
 815   while (next != NULL) {
 816     ClassLoaderData* purge_me = next;
 817     next = purge_me->next();
 818     delete purge_me;
 819   }
 820   Metaspace::purge();
 821 }
 822 
 823 void ClassLoaderDataGraph::post_class_unload_events(void) {
 824 #if INCLUDE_TRACE
 825   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 826   if (Tracing::enabled()) {
 827     if (Tracing::is_event_enabled(TraceClassUnloadEvent)) {
 828       assert(_unloading != NULL, "need class loader data unload list!");
 829       _class_unload_time = Ticks::now();
 830       classes_unloading_do(&class_unload_event);
 831     }
 832     Tracing::on_unloading_classes();
 833   }
 834 #endif
 835 }
 836 
 837 void ClassLoaderDataGraph::free_deallocate_lists() {
 838   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
 839     // We need to keep this data until InstanceKlass::purge_previous_version has been
 840     // called on all alive classes. See the comment in ClassLoaderDataGraph::clean_metaspaces.
 841     cld->free_deallocate_list();
 842   }
 843 
 844   // In some rare cases items added to the unloading list will not be freed elsewhere.
 845   // To keep it simple, walk the _unloading list also.
 846   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
 847     cld->free_deallocate_list();
 848   }
 849 }
 850 
 851 // CDS support
 852 
 853 // Global metaspaces for writing information to the shared archive.  When
 854 // application CDS is supported, we may need one per metaspace, so this
 855 // sort of looks like it.
 856 Metaspace* ClassLoaderData::_ro_metaspace = NULL;


 952     data->verify();
 953   }
 954 }
 955 
 956 void ClassLoaderDataGraph::dump_on(outputStream * const out) {
 957   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
 958     data->dump(out);
 959   }
 960   MetaspaceAux::dump(out);
 961 }
 962 #endif // PRODUCT
 963 
 964 void ClassLoaderData::print_value_on(outputStream* out) const {
 965   if (class_loader() == NULL) {
 966     out->print("NULL class_loader");
 967   } else {
 968     out->print("class loader " INTPTR_FORMAT, p2i(this));
 969     class_loader()->print_value_on(out);
 970   }
 971 }
 972 
 973 #if INCLUDE_TRACE
 974 
 975 Ticks ClassLoaderDataGraph::_class_unload_time;
 976 
 977 void ClassLoaderDataGraph::class_unload_event(Klass* const k) {
 978 
 979   // post class unload event
 980   EventClassUnload event(UNTIMED);
 981   event.set_endtime(_class_unload_time);
 982   event.set_unloadedClass(k);
 983   oop defining_class_loader = k->class_loader();
 984   event.set_definingClassLoader(defining_class_loader != NULL ?
 985                                 defining_class_loader->klass() : (Klass*)NULL);
 986   event.commit();
 987 }
 988 
 989 #endif // INCLUDE_TRACE


  47 // the singleton class the_null_class_loader_data().
  48 
  49 #include "precompiled.hpp"
  50 #include "classfile/classLoaderData.hpp"
  51 #include "classfile/classLoaderData.inline.hpp"
  52 #include "classfile/javaClasses.hpp"
  53 #include "classfile/metadataOnStackMark.hpp"
  54 #include "classfile/systemDictionary.hpp"
  55 #include "code/codeCache.hpp"
  56 #include "memory/gcLocker.hpp"
  57 #include "memory/metadataFactory.hpp"
  58 #include "memory/metaspaceShared.hpp"
  59 #include "memory/oopFactory.hpp"
  60 #include "runtime/jniHandles.hpp"
  61 #include "runtime/mutex.hpp"
  62 #include "runtime/safepoint.hpp"
  63 #include "runtime/synchronizer.hpp"
  64 #include "utilities/growableArray.hpp"
  65 #include "utilities/macros.hpp"
  66 #include "utilities/ostream.hpp"



  67 
  68 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
  69 
  70 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) :
  71   _class_loader(h_class_loader()),
  72   _is_anonymous(is_anonymous),
  73   // An anonymous class loader data doesn't have anything to keep
  74   // it from being unloaded during parsing of the anonymous class.
  75   // The null-class-loader should always be kept alive.
  76   _keep_alive(is_anonymous || h_class_loader.is_null()),
  77   _metaspace(NULL), _unloading(false), _klasses(NULL),
  78   _claimed(0), _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),
  79   _next(NULL), _dependencies(dependencies),
  80   _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {
  81 
  82   JFR_ONLY(INIT_ID(this);)
  83 }
  84 
  85 void ClassLoaderData::init_dependencies(TRAPS) {
  86   assert(!Universe::is_fully_initialized(), "should only be called when initializing");
  87   assert(is_the_null_class_loader_data(), "should only call this for the null class loader");
  88   _dependencies.init(CHECK);
  89 }
  90 
  91 void ClassLoaderData::Dependencies::init(TRAPS) {
  92   // Create empty dependencies array to add to. CMS requires this to be
  93   // an oop so that it can track additions via card marks.  We think.
  94   _list_head = oopFactory::new_objectArray(2, CHECK);
  95 }
  96 
  97 ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() {
  98   Chunk* c = _head;
  99   while (c != NULL) {
 100     Chunk* next = c->_next;
 101     delete c;
 102     c = next;


 627     if (cld->keep_alive()) {
 628       cld->oops_do(f, klass_closure, must_claim);
 629     }
 630   }
 631 }
 632 
 633 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
 634   if (ClassUnloading) {
 635     keep_alive_oops_do(f, klass_closure, must_claim);
 636   } else {
 637     oops_do(f, klass_closure, must_claim);
 638   }
 639 }
 640 
 641 void ClassLoaderDataGraph::cld_do(CLDClosure* cl) {
 642   for (ClassLoaderData* cld = _head; cl != NULL && cld != NULL; cld = cld->next()) {
 643     cl->do_cld(cld);
 644   }
 645 }
 646 
 647 void ClassLoaderDataGraph::cld_unloading_do(CLDClosure* cl) {
 648   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 649   // Only walk the head until any clds not purged from prior unloading
 650   // (CMS doesn't purge right away).
 651   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
 652     assert(cld->is_unloading(), "invariant");
 653     cl->do_cld(cld);
 654   }
 655 }
 656 
 657 void ClassLoaderDataGraph::roots_cld_do(CLDClosure* strong, CLDClosure* weak) {
 658   for (ClassLoaderData* cld = _head;  cld != NULL; cld = cld->_next) {
 659     CLDClosure* closure = cld->keep_alive() ? strong : weak;
 660     if (closure != NULL) {
 661       closure->do_cld(cld);
 662     }
 663   }
 664 }
 665 
 666 void ClassLoaderDataGraph::keep_alive_cld_do(CLDClosure* cl) {
 667   roots_cld_do(cl, NULL);
 668 }
 669 
 670 void ClassLoaderDataGraph::always_strong_cld_do(CLDClosure* cl) {
 671   if (ClassUnloading) {
 672     keep_alive_cld_do(cl);
 673   } else {
 674     cld_do(cl);
 675   }
 676 }


 731   for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) {
 732     if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {
 733       return true;
 734     }
 735   }
 736   return false;
 737 }
 738 
 739 #ifndef PRODUCT
 740 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {
 741   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
 742     if (loader_data == data) {
 743       return true;
 744     }
 745   }
 746 
 747   return false;
 748 }
 749 #endif // PRODUCT
 750 

 751 // Move class loader data from main list to the unloaded list for unloading
 752 // and deallocation later.
 753 bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive_closure, bool clean_alive) {
 754   ClassLoaderData* data = _head;
 755   ClassLoaderData* prev = NULL;
 756   bool seen_dead_loader = false;
 757 
 758   // Save previous _unloading pointer for CMS which may add to unloading list before
 759   // purging and we don't want to rewalk the previously unloaded class loader data.
 760   _saved_unloading = _unloading;
 761 
 762   while (data != NULL) {
 763     if (data->is_alive(is_alive_closure)) {
 764       prev = data;
 765       data = data->next();
 766       continue;
 767     }
 768     seen_dead_loader = true;
 769     ClassLoaderData* dead = data;
 770     dead->unload();
 771     data = data->next();
 772     // Remove from loader list.
 773     // This class loader data will no longer be found
 774     // in the ClassLoaderDataGraph.
 775     if (prev != NULL) {
 776       prev->set_next(data);
 777     } else {
 778       assert(dead == _head, "sanity check");
 779       _head = data;
 780     }
 781     dead->set_next(_unloading);
 782     _unloading = dead;
 783   }
 784 
 785   if (clean_alive) {
 786     // Clean previous versions and the deallocate list.
 787     ClassLoaderDataGraph::clean_metaspaces();
 788   }
 789 




 790   return seen_dead_loader;
 791 }
 792 
 793 void ClassLoaderDataGraph::clean_metaspaces() {
 794   // mark metadata seen on the stack and code cache so we can delete unneeded entries.
 795   bool has_redefined_a_class = JvmtiExport::has_redefined_a_class();
 796   MetadataOnStackMark md_on_stack(has_redefined_a_class);
 797 
 798   if (has_redefined_a_class) {
 799     // purge_previous_versions also cleans weak method links. Because
 800     // one method's MDO can reference another method from another
 801     // class loader, we need to first clean weak method links for all
 802     // class loaders here. Below, we can then free redefined methods
 803     // for all class loaders.
 804     for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
 805       data->classes_do(InstanceKlass::purge_previous_versions);
 806     }
 807   }
 808 
 809   // Need to purge the previous version before deallocating.
 810   free_deallocate_lists();
 811 }
 812 
 813 void ClassLoaderDataGraph::purge() {
 814   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 815   ClassLoaderData* list = _unloading;
 816   _unloading = NULL;
 817   ClassLoaderData* next = list;
 818   while (next != NULL) {
 819     ClassLoaderData* purge_me = next;
 820     next = purge_me->next();
 821     delete purge_me;
 822   }
 823   Metaspace::purge();
 824 }
 825 














 826 void ClassLoaderDataGraph::free_deallocate_lists() {
 827   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
 828     // We need to keep this data until InstanceKlass::purge_previous_version has been
 829     // called on all alive classes. See the comment in ClassLoaderDataGraph::clean_metaspaces.
 830     cld->free_deallocate_list();
 831   }
 832 
 833   // In some rare cases items added to the unloading list will not be freed elsewhere.
 834   // To keep it simple, walk the _unloading list also.
 835   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
 836     cld->free_deallocate_list();
 837   }
 838 }
 839 
 840 // CDS support
 841 
 842 // Global metaspaces for writing information to the shared archive.  When
 843 // application CDS is supported, we may need one per metaspace, so this
 844 // sort of looks like it.
 845 Metaspace* ClassLoaderData::_ro_metaspace = NULL;


 941     data->verify();
 942   }
 943 }
 944 
 945 void ClassLoaderDataGraph::dump_on(outputStream * const out) {
 946   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
 947     data->dump(out);
 948   }
 949   MetaspaceAux::dump(out);
 950 }
 951 #endif // PRODUCT
 952 
 953 void ClassLoaderData::print_value_on(outputStream* out) const {
 954   if (class_loader() == NULL) {
 955     out->print("NULL class_loader");
 956   } else {
 957     out->print("class loader " INTPTR_FORMAT, p2i(this));
 958     class_loader()->print_value_on(out);
 959   }
 960 }


















< prev index next >