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;
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 #include "utilities/ticks.hpp"
68 #if INCLUDE_JFR
69 #include "jfr/jfr.hpp"
70 #include "jfr/jfrEvents.hpp"
71 #endif
72
73 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
74
75 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) :
76 _class_loader(h_class_loader()),
77 _is_anonymous(is_anonymous),
78 // An anonymous class loader data doesn't have anything to keep
79 // it from being unloaded during parsing of the anonymous class.
80 // The null-class-loader should always be kept alive.
81 _keep_alive(is_anonymous || h_class_loader.is_null()),
82 _metaspace(NULL), _unloading(false), _klasses(NULL),
83 _claimed(0), _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),
84 _next(NULL), _dependencies(dependencies),
85 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {
86
87 JFR_ONLY(INIT_ID(this);)
88 }
89
90 void ClassLoaderData::init_dependencies(TRAPS) {
91 assert(!Universe::is_fully_initialized(), "should only be called when initializing");
92 assert(is_the_null_class_loader_data(), "should only call this for the null class loader");
93 _dependencies.init(CHECK);
94 }
95
96 void ClassLoaderData::Dependencies::init(TRAPS) {
97 // Create empty dependencies array to add to. CMS requires this to be
98 // an oop so that it can track additions via card marks. We think.
99 _list_head = oopFactory::new_objectArray(2, CHECK);
100 }
101
102 ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() {
103 Chunk* c = _head;
104 while (c != NULL) {
105 Chunk* next = c->_next;
106 delete c;
107 c = next;
632 if (cld->keep_alive()) {
633 cld->oops_do(f, klass_closure, must_claim);
634 }
635 }
636 }
637
638 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
639 if (ClassUnloading) {
640 keep_alive_oops_do(f, klass_closure, must_claim);
641 } else {
642 oops_do(f, klass_closure, must_claim);
643 }
644 }
645
646 void ClassLoaderDataGraph::cld_do(CLDClosure* cl) {
647 for (ClassLoaderData* cld = _head; cl != NULL && cld != NULL; cld = cld->next()) {
648 cl->do_cld(cld);
649 }
650 }
651
652 void ClassLoaderDataGraph::cld_unloading_do(CLDClosure* cl) {
653 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
654 // Only walk the head until any clds not purged from prior unloading
655 // (CMS doesn't purge right away).
656 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
657 assert(cld->is_unloading(), "invariant");
658 cl->do_cld(cld);
659 }
660 }
661
662 void ClassLoaderDataGraph::roots_cld_do(CLDClosure* strong, CLDClosure* weak) {
663 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->_next) {
664 CLDClosure* closure = cld->keep_alive() ? strong : weak;
665 if (closure != NULL) {
666 closure->do_cld(cld);
667 }
668 }
669 }
670
671 void ClassLoaderDataGraph::keep_alive_cld_do(CLDClosure* cl) {
672 roots_cld_do(cl, NULL);
673 }
674
675 void ClassLoaderDataGraph::always_strong_cld_do(CLDClosure* cl) {
676 if (ClassUnloading) {
677 keep_alive_cld_do(cl);
678 } else {
679 cld_do(cl);
680 }
681 }
736 for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) {
737 if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {
738 return true;
739 }
740 }
741 return false;
742 }
743
744 #ifndef PRODUCT
745 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {
746 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
747 if (loader_data == data) {
748 return true;
749 }
750 }
751
752 return false;
753 }
754 #endif // PRODUCT
755
756 #if INCLUDE_JFR
757 static Ticks class_unload_time;
758 static void post_class_unload_event(Klass* const k) {
759 assert(k != NULL, "invariant");
760 EventClassUnload event(UNTIMED);
761 event.set_endtime(class_unload_time);
762 event.set_unloadedClass(k);
763 event.set_definingClassLoader(k->class_loader_data());
764 event.commit();
765 }
766
767 static void post_class_unload_events() {
768 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
769 if (Jfr::is_enabled()) {
770 if (EventClassUnload::is_enabled()) {
771 class_unload_time = Ticks::now();
772 ClassLoaderDataGraph::classes_unloading_do(&post_class_unload_event);
773 }
774 Jfr::on_unloading_classes();
775 }
776 }
777 #endif // INCLUDE_JFR
778
779 // Move class loader data from main list to the unloaded list for unloading
780 // and deallocation later.
781 bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive_closure, bool clean_alive) {
782 ClassLoaderData* data = _head;
783 ClassLoaderData* prev = NULL;
784 bool seen_dead_loader = false;
785
786 // Save previous _unloading pointer for CMS which may add to unloading list before
787 // purging and we don't want to rewalk the previously unloaded class loader data.
788 _saved_unloading = _unloading;
789
790 while (data != NULL) {
791 if (data->is_alive(is_alive_closure)) {
792 prev = data;
793 data = data->next();
794 continue;
795 }
796 seen_dead_loader = true;
797 ClassLoaderData* dead = data;
799 data = data->next();
800 // Remove from loader list.
801 // This class loader data will no longer be found
802 // in the ClassLoaderDataGraph.
803 if (prev != NULL) {
804 prev->set_next(data);
805 } else {
806 assert(dead == _head, "sanity check");
807 _head = data;
808 }
809 dead->set_next(_unloading);
810 _unloading = dead;
811 }
812
813 if (clean_alive) {
814 // Clean previous versions and the deallocate list.
815 ClassLoaderDataGraph::clean_metaspaces();
816 }
817
818 if (seen_dead_loader) {
819 JFR_ONLY(post_class_unload_events();)
820 }
821
822 return seen_dead_loader;
823 }
824
825 void ClassLoaderDataGraph::clean_metaspaces() {
826 // mark metadata seen on the stack and code cache so we can delete unneeded entries.
827 bool has_redefined_a_class = JvmtiExport::has_redefined_a_class();
828 MetadataOnStackMark md_on_stack(has_redefined_a_class);
829
830 if (has_redefined_a_class) {
831 // purge_previous_versions also cleans weak method links. Because
832 // one method's MDO can reference another method from another
833 // class loader, we need to first clean weak method links for all
834 // class loaders here. Below, we can then free redefined methods
835 // for all class loaders.
836 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
837 data->classes_do(InstanceKlass::purge_previous_versions);
838 }
839 }
840
841 // Need to purge the previous version before deallocating.
842 free_deallocate_lists();
843 }
844
845 void ClassLoaderDataGraph::purge() {
846 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
847 ClassLoaderData* list = _unloading;
848 _unloading = NULL;
849 ClassLoaderData* next = list;
850 while (next != NULL) {
851 ClassLoaderData* purge_me = next;
852 next = purge_me->next();
853 delete purge_me;
854 }
855 Metaspace::purge();
856 }
857
858 void ClassLoaderDataGraph::free_deallocate_lists() {
859 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
860 // We need to keep this data until InstanceKlass::purge_previous_version has been
861 // called on all alive classes. See the comment in ClassLoaderDataGraph::clean_metaspaces.
862 cld->free_deallocate_list();
863 }
864
865 // In some rare cases items added to the unloading list will not be freed elsewhere.
866 // To keep it simple, walk the _unloading list also.
867 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
868 cld->free_deallocate_list();
869 }
870 }
871
872 // CDS support
873
874 // Global metaspaces for writing information to the shared archive. When
875 // application CDS is supported, we may need one per metaspace, so this
876 // sort of looks like it.
877 Metaspace* ClassLoaderData::_ro_metaspace = NULL;
973 data->verify();
974 }
975 }
976
977 void ClassLoaderDataGraph::dump_on(outputStream * const out) {
978 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
979 data->dump(out);
980 }
981 MetaspaceAux::dump(out);
982 }
983 #endif // PRODUCT
984
985 void ClassLoaderData::print_value_on(outputStream* out) const {
986 if (class_loader() == NULL) {
987 out->print("NULL class_loader");
988 } else {
989 out->print("class loader " INTPTR_FORMAT, p2i(this));
990 class_loader()->print_value_on(out);
991 }
992 }
|