49 #include "cds/heapShared.hpp"
50 #include "classfile/classLoaderData.inline.hpp"
51 #include "classfile/classLoaderDataGraph.inline.hpp"
52 #include "classfile/dictionary.hpp"
53 #include "classfile/javaClasses.inline.hpp"
54 #include "classfile/moduleEntry.hpp"
55 #include "classfile/packageEntry.hpp"
56 #include "classfile/symbolTable.hpp"
57 #include "classfile/systemDictionary.hpp"
58 #include "classfile/systemDictionaryShared.hpp"
59 #include "classfile/vmClasses.hpp"
60 #include "logging/log.hpp"
61 #include "logging/logStream.hpp"
62 #include "memory/allocation.inline.hpp"
63 #include "memory/classLoaderMetaspace.hpp"
64 #include "memory/metadataFactory.hpp"
65 #include "memory/metaspace.hpp"
66 #include "memory/resourceArea.hpp"
67 #include "memory/universe.hpp"
68 #include "oops/access.inline.hpp"
69 #include "oops/jmethodIDTable.hpp"
70 #include "oops/klass.inline.hpp"
71 #include "oops/oop.inline.hpp"
72 #include "oops/oopHandle.inline.hpp"
73 #include "oops/verifyOopClosure.hpp"
74 #include "oops/weakHandle.inline.hpp"
75 #include "runtime/arguments.hpp"
76 #include "runtime/atomicAccess.hpp"
77 #include "runtime/handles.inline.hpp"
78 #include "runtime/mutex.hpp"
79 #include "runtime/safepoint.hpp"
80 #include "utilities/growableArray.hpp"
81 #include "utilities/macros.hpp"
82 #include "utilities/ostream.hpp"
83
84 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = nullptr;
85
86 void ClassLoaderData::init_null_class_loader_data() {
87 assert(_the_null_class_loader_data == nullptr, "cannot initialize twice");
88 assert(ClassLoaderDataGraph::_head == nullptr, "cannot initialize twice");
411
412 #ifdef ASSERT
413 oop m = k->java_mirror();
414 assert(m != nullptr, "nullptr mirror");
415 assert(m->is_a(vmClasses::Class_klass()), "invalid mirror");
416 #endif
417 klass_closure->do_klass(k);
418 }
419 }
420
421 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
422 // Lock-free access requires load_acquire
423 for (Klass* k = AtomicAccess::load_acquire(&_klasses); k != nullptr; k = k->next_link()) {
424 if (k->is_instance_klass()) {
425 f(InstanceKlass::cast(k));
426 }
427 assert(k != k->next_link(), "no loops!");
428 }
429 }
430
431 void ClassLoaderData::modules_do(void f(ModuleEntry*)) {
432 assert_locked_or_safepoint(Module_lock);
433 if (_unnamed_module != nullptr) {
434 f(_unnamed_module);
435 }
436 if (_modules != nullptr) {
437 _modules->modules_do(f);
438 }
439 }
440
441 void ClassLoaderData::packages_do(void f(PackageEntry*)) {
442 assert_locked_or_safepoint(Module_lock);
443 if (_packages != nullptr) {
444 _packages->packages_do(f);
445 }
446 }
447
448 void ClassLoaderData::record_dependency(const Klass* k) {
449 assert(k != nullptr, "invariant");
450
589 delete _jmethod_ids;
590 _jmethod_ids = nullptr;
591 }
592
593 void ClassLoaderData::unload() {
594 _unloading = true;
595
596 LogTarget(Trace, class, loader, data) lt;
597 if (lt.is_enabled()) {
598 ResourceMark rm;
599 LogStream ls(lt);
600 ls.print("unload");
601 print_value_on(&ls);
602 ls.cr();
603 }
604
605 // Some items on the _deallocate_list need to free their C heap structures
606 // if they are not already on the _klasses list.
607 free_deallocate_list_C_heap_structures();
608
609 // Clean up class dependencies and tell serviceability tools
610 // these classes are unloading. This must be called
611 // after erroneous classes are released.
612 classes_do(InstanceKlass::unload_class);
613
614 if (_jmethod_ids != nullptr) {
615 remove_jmethod_ids();
616 }
617 }
618
619 ModuleEntryTable* ClassLoaderData::modules() {
620 // Lazily create the module entry table at first request.
621 // Lock-free access requires load_acquire.
622 ModuleEntryTable* modules = AtomicAccess::load_acquire(&_modules);
623 if (modules == nullptr) {
624 MutexLocker m1(Module_lock);
625 // Check if _modules got allocated while we were waiting for this lock.
626 if ((modules = _modules) == nullptr) {
627 modules = new ModuleEntryTable();
628
869 // This must be called at a safepoint because it depends on metadata walking at
870 // safepoint cleanup time.
871 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
872 assert(!is_unloading(), "only called for ClassLoaderData that are not unloading");
873 if (_deallocate_list == nullptr) {
874 return;
875 }
876 // Go backwards because this removes entries that are freed.
877 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
878 Metadata* m = _deallocate_list->at(i);
879 if (!m->on_stack()) {
880 _deallocate_list->remove_at(i);
881 // There are only three types of metadata that we deallocate directly.
882 // Cast them so they can be used by the template function.
883 if (m->is_method()) {
884 MetadataFactory::free_metadata(this, (Method*)m);
885 } else if (m->is_constantPool()) {
886 HeapShared::remove_scratch_resolved_references((ConstantPool*)m);
887 MetadataFactory::free_metadata(this, (ConstantPool*)m);
888 } else if (m->is_klass()) {
889 MetadataFactory::free_metadata(this, (InstanceKlass*)m);
890 } else {
891 ShouldNotReachHere();
892 }
893 } else {
894 // Metadata is alive.
895 // If scratch_class is on stack then it shouldn't be on this list!
896 assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(),
897 "scratch classes on this list should be dead");
898 // Also should assert that other metadata on the list was found in handles.
899 // Some cleaning remains.
900 ClassLoaderDataGraph::set_should_clean_deallocate_lists();
901 }
902 }
903 }
904
905 // This is distinct from free_deallocate_list. For class loader data that are
906 // unloading, this frees the C heap memory for items on the list, and unlinks
907 // scratch or error classes so that unloading events aren't triggered for these
908 // classes. The metadata is removed with the unloading metaspace.
909 // There isn't C heap memory allocated for methods, so nothing is done for them.
|
49 #include "cds/heapShared.hpp"
50 #include "classfile/classLoaderData.inline.hpp"
51 #include "classfile/classLoaderDataGraph.inline.hpp"
52 #include "classfile/dictionary.hpp"
53 #include "classfile/javaClasses.inline.hpp"
54 #include "classfile/moduleEntry.hpp"
55 #include "classfile/packageEntry.hpp"
56 #include "classfile/symbolTable.hpp"
57 #include "classfile/systemDictionary.hpp"
58 #include "classfile/systemDictionaryShared.hpp"
59 #include "classfile/vmClasses.hpp"
60 #include "logging/log.hpp"
61 #include "logging/logStream.hpp"
62 #include "memory/allocation.inline.hpp"
63 #include "memory/classLoaderMetaspace.hpp"
64 #include "memory/metadataFactory.hpp"
65 #include "memory/metaspace.hpp"
66 #include "memory/resourceArea.hpp"
67 #include "memory/universe.hpp"
68 #include "oops/access.inline.hpp"
69 #include "oops/inlineKlass.inline.hpp"
70 #include "oops/jmethodIDTable.hpp"
71 #include "oops/klass.inline.hpp"
72 #include "oops/oop.inline.hpp"
73 #include "oops/oopHandle.inline.hpp"
74 #include "oops/verifyOopClosure.hpp"
75 #include "oops/weakHandle.inline.hpp"
76 #include "runtime/arguments.hpp"
77 #include "runtime/atomicAccess.hpp"
78 #include "runtime/handles.inline.hpp"
79 #include "runtime/mutex.hpp"
80 #include "runtime/safepoint.hpp"
81 #include "utilities/growableArray.hpp"
82 #include "utilities/macros.hpp"
83 #include "utilities/ostream.hpp"
84
85 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = nullptr;
86
87 void ClassLoaderData::init_null_class_loader_data() {
88 assert(_the_null_class_loader_data == nullptr, "cannot initialize twice");
89 assert(ClassLoaderDataGraph::_head == nullptr, "cannot initialize twice");
412
413 #ifdef ASSERT
414 oop m = k->java_mirror();
415 assert(m != nullptr, "nullptr mirror");
416 assert(m->is_a(vmClasses::Class_klass()), "invalid mirror");
417 #endif
418 klass_closure->do_klass(k);
419 }
420 }
421
422 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
423 // Lock-free access requires load_acquire
424 for (Klass* k = AtomicAccess::load_acquire(&_klasses); k != nullptr; k = k->next_link()) {
425 if (k->is_instance_klass()) {
426 f(InstanceKlass::cast(k));
427 }
428 assert(k != k->next_link(), "no loops!");
429 }
430 }
431
432 void ClassLoaderData::inline_classes_do(void f(InlineKlass*)) {
433 // Lock-free access requires load_acquire
434 for (Klass* k = AtomicAccess::load_acquire(&_klasses); k != nullptr; k = k->next_link()) {
435 if (k->is_inline_klass()) {
436 f(InlineKlass::cast(k));
437 }
438 assert(k != k->next_link(), "no loops!");
439 }
440 }
441
442 void ClassLoaderData::modules_do(void f(ModuleEntry*)) {
443 assert_locked_or_safepoint(Module_lock);
444 if (_unnamed_module != nullptr) {
445 f(_unnamed_module);
446 }
447 if (_modules != nullptr) {
448 _modules->modules_do(f);
449 }
450 }
451
452 void ClassLoaderData::packages_do(void f(PackageEntry*)) {
453 assert_locked_or_safepoint(Module_lock);
454 if (_packages != nullptr) {
455 _packages->packages_do(f);
456 }
457 }
458
459 void ClassLoaderData::record_dependency(const Klass* k) {
460 assert(k != nullptr, "invariant");
461
600 delete _jmethod_ids;
601 _jmethod_ids = nullptr;
602 }
603
604 void ClassLoaderData::unload() {
605 _unloading = true;
606
607 LogTarget(Trace, class, loader, data) lt;
608 if (lt.is_enabled()) {
609 ResourceMark rm;
610 LogStream ls(lt);
611 ls.print("unload");
612 print_value_on(&ls);
613 ls.cr();
614 }
615
616 // Some items on the _deallocate_list need to free their C heap structures
617 // if they are not already on the _klasses list.
618 free_deallocate_list_C_heap_structures();
619
620 inline_classes_do(InlineKlass::cleanup);
621
622 // Clean up class dependencies and tell serviceability tools
623 // these classes are unloading. This must be called
624 // after erroneous classes are released.
625 classes_do(InstanceKlass::unload_class);
626
627 if (_jmethod_ids != nullptr) {
628 remove_jmethod_ids();
629 }
630 }
631
632 ModuleEntryTable* ClassLoaderData::modules() {
633 // Lazily create the module entry table at first request.
634 // Lock-free access requires load_acquire.
635 ModuleEntryTable* modules = AtomicAccess::load_acquire(&_modules);
636 if (modules == nullptr) {
637 MutexLocker m1(Module_lock);
638 // Check if _modules got allocated while we were waiting for this lock.
639 if ((modules = _modules) == nullptr) {
640 modules = new ModuleEntryTable();
641
882 // This must be called at a safepoint because it depends on metadata walking at
883 // safepoint cleanup time.
884 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
885 assert(!is_unloading(), "only called for ClassLoaderData that are not unloading");
886 if (_deallocate_list == nullptr) {
887 return;
888 }
889 // Go backwards because this removes entries that are freed.
890 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
891 Metadata* m = _deallocate_list->at(i);
892 if (!m->on_stack()) {
893 _deallocate_list->remove_at(i);
894 // There are only three types of metadata that we deallocate directly.
895 // Cast them so they can be used by the template function.
896 if (m->is_method()) {
897 MetadataFactory::free_metadata(this, (Method*)m);
898 } else if (m->is_constantPool()) {
899 HeapShared::remove_scratch_resolved_references((ConstantPool*)m);
900 MetadataFactory::free_metadata(this, (ConstantPool*)m);
901 } else if (m->is_klass()) {
902 if (!((Klass*)m)->is_inline_klass()) {
903 MetadataFactory::free_metadata(this, (InstanceKlass*)m);
904 } else {
905 MetadataFactory::free_metadata(this, (InlineKlass*)m);
906 }
907 } else {
908 ShouldNotReachHere();
909 }
910 } else {
911 // Metadata is alive.
912 // If scratch_class is on stack then it shouldn't be on this list!
913 assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(),
914 "scratch classes on this list should be dead");
915 // Also should assert that other metadata on the list was found in handles.
916 // Some cleaning remains.
917 ClassLoaderDataGraph::set_should_clean_deallocate_lists();
918 }
919 }
920 }
921
922 // This is distinct from free_deallocate_list. For class loader data that are
923 // unloading, this frees the C heap memory for items on the list, and unlinks
924 // scratch or error classes so that unloading events aren't triggered for these
925 // classes. The metadata is removed with the unloading metaspace.
926 // There isn't C heap memory allocated for methods, so nothing is done for them.
|