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