48
49 #include "classfile/classLoaderData.inline.hpp"
50 #include "classfile/classLoaderDataGraph.inline.hpp"
51 #include "classfile/dictionary.hpp"
52 #include "classfile/javaClasses.inline.hpp"
53 #include "classfile/moduleEntry.hpp"
54 #include "classfile/packageEntry.hpp"
55 #include "classfile/symbolTable.hpp"
56 #include "classfile/systemDictionary.hpp"
57 #include "classfile/systemDictionaryShared.hpp"
58 #include "classfile/vmClasses.hpp"
59 #include "logging/log.hpp"
60 #include "logging/logStream.hpp"
61 #include "memory/allocation.inline.hpp"
62 #include "memory/classLoaderMetaspace.hpp"
63 #include "memory/metadataFactory.hpp"
64 #include "memory/metaspace.hpp"
65 #include "memory/resourceArea.hpp"
66 #include "memory/universe.hpp"
67 #include "oops/access.inline.hpp"
68 #include "oops/jmethodIDTable.hpp"
69 #include "oops/klass.inline.hpp"
70 #include "oops/oop.inline.hpp"
71 #include "oops/oopHandle.inline.hpp"
72 #include "oops/verifyOopClosure.hpp"
73 #include "oops/weakHandle.inline.hpp"
74 #include "runtime/arguments.hpp"
75 #include "runtime/atomic.hpp"
76 #include "runtime/handles.inline.hpp"
77 #include "runtime/mutex.hpp"
78 #include "runtime/safepoint.hpp"
79 #include "utilities/growableArray.hpp"
80 #include "utilities/macros.hpp"
81 #include "utilities/ostream.hpp"
82
83 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = nullptr;
84
85 void ClassLoaderData::init_null_class_loader_data() {
86 assert(_the_null_class_loader_data == nullptr, "cannot initialize twice");
87 assert(ClassLoaderDataGraph::_head == nullptr, "cannot initialize twice");
427
428 #ifdef ASSERT
429 oop m = k->java_mirror();
430 assert(m != nullptr, "nullptr mirror");
431 assert(m->is_a(vmClasses::Class_klass()), "invalid mirror");
432 #endif
433 klass_closure->do_klass(k);
434 }
435 }
436
437 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
438 // Lock-free access requires load_acquire
439 for (Klass* k = Atomic::load_acquire(&_klasses); k != nullptr; k = k->next_link()) {
440 if (k->is_instance_klass()) {
441 f(InstanceKlass::cast(k));
442 }
443 assert(k != k->next_link(), "no loops!");
444 }
445 }
446
447 void ClassLoaderData::modules_do(void f(ModuleEntry*)) {
448 assert_locked_or_safepoint(Module_lock);
449 if (_unnamed_module != nullptr) {
450 f(_unnamed_module);
451 }
452 if (_modules != nullptr) {
453 _modules->modules_do(f);
454 }
455 }
456
457 void ClassLoaderData::packages_do(void f(PackageEntry*)) {
458 assert_locked_or_safepoint(Module_lock);
459 if (_packages != nullptr) {
460 _packages->packages_do(f);
461 }
462 }
463
464 void ClassLoaderData::record_dependency(const Klass* k) {
465 assert(k != nullptr, "invariant");
466
605 delete _jmethod_ids;
606 _jmethod_ids = nullptr;
607 }
608
609 void ClassLoaderData::unload() {
610 _unloading = true;
611
612 LogTarget(Trace, class, loader, data) lt;
613 if (lt.is_enabled()) {
614 ResourceMark rm;
615 LogStream ls(lt);
616 ls.print("unload");
617 print_value_on(&ls);
618 ls.cr();
619 }
620
621 // Some items on the _deallocate_list need to free their C heap structures
622 // if they are not already on the _klasses list.
623 free_deallocate_list_C_heap_structures();
624
625 // Clean up class dependencies and tell serviceability tools
626 // these classes are unloading. This must be called
627 // after erroneous classes are released.
628 classes_do(InstanceKlass::unload_class);
629
630 if (_jmethod_ids != nullptr) {
631 remove_jmethod_ids();
632 }
633 }
634
635 ModuleEntryTable* ClassLoaderData::modules() {
636 // Lazily create the module entry table at first request.
637 // Lock-free access requires load_acquire.
638 ModuleEntryTable* modules = Atomic::load_acquire(&_modules);
639 if (modules == nullptr) {
640 MutexLocker m1(Module_lock);
641 // Check if _modules got allocated while we were waiting for this lock.
642 if ((modules = _modules) == nullptr) {
643 modules = new ModuleEntryTable();
644
884 void ClassLoaderData::free_deallocate_list() {
885 // This must be called at a safepoint because it depends on metadata walking at
886 // safepoint cleanup time.
887 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
888 assert(!is_unloading(), "only called for ClassLoaderData that are not unloading");
889 if (_deallocate_list == nullptr) {
890 return;
891 }
892 // Go backwards because this removes entries that are freed.
893 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
894 Metadata* m = _deallocate_list->at(i);
895 if (!m->on_stack()) {
896 _deallocate_list->remove_at(i);
897 // There are only three types of metadata that we deallocate directly.
898 // Cast them so they can be used by the template function.
899 if (m->is_method()) {
900 MetadataFactory::free_metadata(this, (Method*)m);
901 } else if (m->is_constantPool()) {
902 MetadataFactory::free_metadata(this, (ConstantPool*)m);
903 } else if (m->is_klass()) {
904 MetadataFactory::free_metadata(this, (InstanceKlass*)m);
905 } else {
906 ShouldNotReachHere();
907 }
908 } else {
909 // Metadata is alive.
910 // If scratch_class is on stack then it shouldn't be on this list!
911 assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(),
912 "scratch classes on this list should be dead");
913 // Also should assert that other metadata on the list was found in handles.
914 // Some cleaning remains.
915 ClassLoaderDataGraph::set_should_clean_deallocate_lists();
916 }
917 }
918 }
919
920 // This is distinct from free_deallocate_list. For class loader data that are
921 // unloading, this frees the C heap memory for items on the list, and unlinks
922 // scratch or error classes so that unloading events aren't triggered for these
923 // classes. The metadata is removed with the unloading metaspace.
924 // There isn't C heap memory allocated for methods, so nothing is done for them.
|
48
49 #include "classfile/classLoaderData.inline.hpp"
50 #include "classfile/classLoaderDataGraph.inline.hpp"
51 #include "classfile/dictionary.hpp"
52 #include "classfile/javaClasses.inline.hpp"
53 #include "classfile/moduleEntry.hpp"
54 #include "classfile/packageEntry.hpp"
55 #include "classfile/symbolTable.hpp"
56 #include "classfile/systemDictionary.hpp"
57 #include "classfile/systemDictionaryShared.hpp"
58 #include "classfile/vmClasses.hpp"
59 #include "logging/log.hpp"
60 #include "logging/logStream.hpp"
61 #include "memory/allocation.inline.hpp"
62 #include "memory/classLoaderMetaspace.hpp"
63 #include "memory/metadataFactory.hpp"
64 #include "memory/metaspace.hpp"
65 #include "memory/resourceArea.hpp"
66 #include "memory/universe.hpp"
67 #include "oops/access.inline.hpp"
68 #include "oops/inlineKlass.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/atomic.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");
428
429 #ifdef ASSERT
430 oop m = k->java_mirror();
431 assert(m != nullptr, "nullptr mirror");
432 assert(m->is_a(vmClasses::Class_klass()), "invalid mirror");
433 #endif
434 klass_closure->do_klass(k);
435 }
436 }
437
438 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
439 // Lock-free access requires load_acquire
440 for (Klass* k = Atomic::load_acquire(&_klasses); k != nullptr; k = k->next_link()) {
441 if (k->is_instance_klass()) {
442 f(InstanceKlass::cast(k));
443 }
444 assert(k != k->next_link(), "no loops!");
445 }
446 }
447
448 void ClassLoaderData::inline_classes_do(void f(InlineKlass*)) {
449 // Lock-free access requires load_acquire
450 for (Klass* k = Atomic::load_acquire(&_klasses); k != nullptr; k = k->next_link()) {
451 if (k->is_inline_klass()) {
452 f(InlineKlass::cast(k));
453 }
454 assert(k != k->next_link(), "no loops!");
455 }
456 }
457
458 void ClassLoaderData::modules_do(void f(ModuleEntry*)) {
459 assert_locked_or_safepoint(Module_lock);
460 if (_unnamed_module != nullptr) {
461 f(_unnamed_module);
462 }
463 if (_modules != nullptr) {
464 _modules->modules_do(f);
465 }
466 }
467
468 void ClassLoaderData::packages_do(void f(PackageEntry*)) {
469 assert_locked_or_safepoint(Module_lock);
470 if (_packages != nullptr) {
471 _packages->packages_do(f);
472 }
473 }
474
475 void ClassLoaderData::record_dependency(const Klass* k) {
476 assert(k != nullptr, "invariant");
477
616 delete _jmethod_ids;
617 _jmethod_ids = nullptr;
618 }
619
620 void ClassLoaderData::unload() {
621 _unloading = true;
622
623 LogTarget(Trace, class, loader, data) lt;
624 if (lt.is_enabled()) {
625 ResourceMark rm;
626 LogStream ls(lt);
627 ls.print("unload");
628 print_value_on(&ls);
629 ls.cr();
630 }
631
632 // Some items on the _deallocate_list need to free their C heap structures
633 // if they are not already on the _klasses list.
634 free_deallocate_list_C_heap_structures();
635
636 inline_classes_do(InlineKlass::cleanup);
637
638 // Clean up class dependencies and tell serviceability tools
639 // these classes are unloading. This must be called
640 // after erroneous classes are released.
641 classes_do(InstanceKlass::unload_class);
642
643 if (_jmethod_ids != nullptr) {
644 remove_jmethod_ids();
645 }
646 }
647
648 ModuleEntryTable* ClassLoaderData::modules() {
649 // Lazily create the module entry table at first request.
650 // Lock-free access requires load_acquire.
651 ModuleEntryTable* modules = Atomic::load_acquire(&_modules);
652 if (modules == nullptr) {
653 MutexLocker m1(Module_lock);
654 // Check if _modules got allocated while we were waiting for this lock.
655 if ((modules = _modules) == nullptr) {
656 modules = new ModuleEntryTable();
657
897 void ClassLoaderData::free_deallocate_list() {
898 // This must be called at a safepoint because it depends on metadata walking at
899 // safepoint cleanup time.
900 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
901 assert(!is_unloading(), "only called for ClassLoaderData that are not unloading");
902 if (_deallocate_list == nullptr) {
903 return;
904 }
905 // Go backwards because this removes entries that are freed.
906 for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
907 Metadata* m = _deallocate_list->at(i);
908 if (!m->on_stack()) {
909 _deallocate_list->remove_at(i);
910 // There are only three types of metadata that we deallocate directly.
911 // Cast them so they can be used by the template function.
912 if (m->is_method()) {
913 MetadataFactory::free_metadata(this, (Method*)m);
914 } else if (m->is_constantPool()) {
915 MetadataFactory::free_metadata(this, (ConstantPool*)m);
916 } else if (m->is_klass()) {
917 if (!((Klass*)m)->is_inline_klass()) {
918 MetadataFactory::free_metadata(this, (InstanceKlass*)m);
919 } else {
920 MetadataFactory::free_metadata(this, (InlineKlass*)m);
921 }
922 } else {
923 ShouldNotReachHere();
924 }
925 } else {
926 // Metadata is alive.
927 // If scratch_class is on stack then it shouldn't be on this list!
928 assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(),
929 "scratch classes on this list should be dead");
930 // Also should assert that other metadata on the list was found in handles.
931 // Some cleaning remains.
932 ClassLoaderDataGraph::set_should_clean_deallocate_lists();
933 }
934 }
935 }
936
937 // This is distinct from free_deallocate_list. For class loader data that are
938 // unloading, this frees the C heap memory for items on the list, and unlinks
939 // scratch or error classes so that unloading events aren't triggered for these
940 // classes. The metadata is removed with the unloading metaspace.
941 // There isn't C heap memory allocated for methods, so nothing is done for them.
|