40 // loader constraints table) as well as the runtime representation of classes
41 // only reference ClassLoaderData.
42 //
43 // Instances of java.lang.ClassLoader holds a pointer to a ClassLoaderData that
44 // that represent the loader's "linking domain" in the JVM.
45 //
46 // The bootstrap loader (represented by NULL) also has a ClassLoaderData,
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/moduleEntry.hpp"
55 #include "classfile/packageEntry.hpp"
56 #include "classfile/systemDictionary.hpp"
57 #include "code/codeCache.hpp"
58 #include "gc/shared/gcLocker.hpp"
59 #include "logging/log.hpp"
60 #include "memory/metadataFactory.hpp"
61 #include "memory/metaspaceShared.hpp"
62 #include "memory/oopFactory.hpp"
63 #include "memory/resourceArea.hpp"
64 #include "oops/objArrayOop.inline.hpp"
65 #include "oops/oop.inline.hpp"
66 #include "runtime/atomic.hpp"
67 #include "runtime/javaCalls.hpp"
68 #include "runtime/jniHandles.hpp"
69 #include "runtime/mutex.hpp"
70 #include "runtime/orderAccess.hpp"
71 #include "runtime/safepoint.hpp"
72 #include "runtime/synchronizer.hpp"
73 #include "utilities/growableArray.hpp"
74 #include "utilities/macros.hpp"
75 #include "utilities/ostream.hpp"
76 #if INCLUDE_TRACE
77 #include "trace/tracing.hpp"
78 #endif
79
459 if (prev == NULL) {
460 _klasses = k->next_link();
461 } else {
462 Klass* next = k->next_link();
463 prev->set_next_link(next);
464 }
465 return;
466 }
467 prev = k;
468 assert(k != k->next_link(), "no loops!");
469 }
470 ShouldNotReachHere(); // should have found this class!!
471 }
472
473 void ClassLoaderData::unload() {
474 _unloading = true;
475
476 // Tell serviceability tools these classes are unloading
477 classes_do(InstanceKlass::notify_unload_class);
478
479 if (log_is_enabled(Debug, class, loader, data)) {
480 ResourceMark rm;
481 outputStream* log = Log(class, loader, data)::debug_stream();
482 log->print(": unload loader data " INTPTR_FORMAT, p2i(this));
483 log->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)class_loader()),
484 loader_name());
485 if (is_anonymous()) {
486 log->print(" for anonymous class " INTPTR_FORMAT " ", p2i(_klasses));
487 }
488 log->cr();
489 }
490
491 // In some rare cases items added to this list will not be freed elsewhere.
492 // To keep it simple, just free everything in it here.
493 free_deallocate_list();
494 }
495
496 ModuleEntryTable* ClassLoaderData::modules() {
497 // Lazily create the module entry table at first request.
498 // Lock-free access requires load_ptr_acquire.
499 ModuleEntryTable* modules = load_ptr_acquire(&_modules);
500 if (modules == NULL) {
501 MutexLocker m1(Module_lock);
502 // Check if _modules got allocated while we were waiting for this lock.
503 if ((modules = _modules) == NULL) {
504 modules = new ModuleEntryTable(ModuleEntryTable::_moduletable_entry_size);
505
506 {
507 MutexLockerEx m1(metaspace_lock(), Mutex::_no_safepoint_check_flag);
508 // Ensure _modules is stable, since it is examined without a lock
786 if (!is_anonymous) {
787 ClassLoaderData** cld_addr = java_lang_ClassLoader::loader_data_addr(loader());
788 // First, Atomically set it
789 ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL);
790 if (old != NULL) {
791 delete cld;
792 // Returns the data.
793 return old;
794 }
795 }
796
797 // We won the race, and therefore the task of adding the data to the list of
798 // class loader data
799 ClassLoaderData** list_head = &_head;
800 ClassLoaderData* next = _head;
801
802 do {
803 cld->set_next(next);
804 ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next);
805 if (exchanged == next) {
806 if (log_is_enabled(Debug, class, loader, data)) {
807 PauseNoSafepointVerifier pnsv(&no_safepoints); // Need safe points for JavaCalls::call_virtual
808 log_creation(loader, cld, CHECK_NULL);
809 }
810 return cld;
811 }
812 next = exchanged;
813 } while (true);
814 }
815
816 void ClassLoaderDataGraph::log_creation(Handle loader, ClassLoaderData* cld, TRAPS) {
817 Handle string;
818 if (loader.not_null()) {
819 // Include the result of loader.toString() in the output. This allows
820 // the user of the log to identify the class loader instance.
821 JavaValue result(T_OBJECT);
822 Klass* spec_klass = SystemDictionary::ClassLoader_klass();
823 JavaCalls::call_virtual(&result,
824 loader,
825 spec_klass,
826 vmSymbols::toString_name(),
827 vmSymbols::void_string_signature(),
828 CHECK);
829 assert(result.get_type() == T_OBJECT, "just checking");
830 string = Handle(THREAD, (oop)result.get_jobject());
831 }
832
833 ResourceMark rm;
834 outputStream* log = Log(class, loader, data)::debug_stream();
835 log->print("create class loader data " INTPTR_FORMAT, p2i(cld));
836 log->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)cld->class_loader()),
837 cld->loader_name());
838
839 if (string.not_null()) {
840 log->print(": ");
841 java_lang_String::print(string(), log);
842 }
843 log->cr();
844 }
845
846
847 void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
848 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
849 cld->oops_do(f, klass_closure, must_claim);
850 }
851 }
852
853 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
854 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
855 if (cld->keep_alive()) {
856 cld->oops_do(f, klass_closure, must_claim);
857 }
858 }
859 }
860
861 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
862 if (ClassUnloading) {
863 keep_alive_oops_do(f, klass_closure, must_claim);
964 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
965 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
966 // Only walk the head until any clds not purged from prior unloading
967 // (CMS doesn't purge right away).
968 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
969 assert(cld->is_unloading(), "invariant");
970 cld->classes_do(f);
971 }
972 }
973
974 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
975 assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
976
977 GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
978
979 // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
980 ClassLoaderData* curr = _head;
981 while (curr != _saved_head) {
982 if (!curr->claimed()) {
983 array->push(curr);
984
985 if (log_is_enabled(Debug, class, loader, data)) {
986 outputStream* log = Log(class, loader, data)::debug_stream();
987 log->print("found new CLD: ");
988 curr->print_value_on(log);
989 log->cr();
990 }
991 }
992
993 curr = curr->_next;
994 }
995
996 return array;
997 }
998
999 bool ClassLoaderDataGraph::unload_list_contains(const void* x) {
1000 assert(SafepointSynchronize::is_at_safepoint(), "only safe to call at safepoint");
1001 for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) {
1002 if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {
1003 return true;
1004 }
1005 }
1006 return false;
1007 }
1008
1009 #ifndef PRODUCT
|
40 // loader constraints table) as well as the runtime representation of classes
41 // only reference ClassLoaderData.
42 //
43 // Instances of java.lang.ClassLoader holds a pointer to a ClassLoaderData that
44 // that represent the loader's "linking domain" in the JVM.
45 //
46 // The bootstrap loader (represented by NULL) also has a ClassLoaderData,
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/moduleEntry.hpp"
55 #include "classfile/packageEntry.hpp"
56 #include "classfile/systemDictionary.hpp"
57 #include "code/codeCache.hpp"
58 #include "gc/shared/gcLocker.hpp"
59 #include "logging/log.hpp"
60 #include "logging/logStream.hpp"
61 #include "memory/metadataFactory.hpp"
62 #include "memory/metaspaceShared.hpp"
63 #include "memory/oopFactory.hpp"
64 #include "memory/resourceArea.hpp"
65 #include "oops/objArrayOop.inline.hpp"
66 #include "oops/oop.inline.hpp"
67 #include "runtime/atomic.hpp"
68 #include "runtime/javaCalls.hpp"
69 #include "runtime/jniHandles.hpp"
70 #include "runtime/mutex.hpp"
71 #include "runtime/orderAccess.hpp"
72 #include "runtime/safepoint.hpp"
73 #include "runtime/synchronizer.hpp"
74 #include "utilities/growableArray.hpp"
75 #include "utilities/macros.hpp"
76 #include "utilities/ostream.hpp"
77 #if INCLUDE_TRACE
78 #include "trace/tracing.hpp"
79 #endif
80
460 if (prev == NULL) {
461 _klasses = k->next_link();
462 } else {
463 Klass* next = k->next_link();
464 prev->set_next_link(next);
465 }
466 return;
467 }
468 prev = k;
469 assert(k != k->next_link(), "no loops!");
470 }
471 ShouldNotReachHere(); // should have found this class!!
472 }
473
474 void ClassLoaderData::unload() {
475 _unloading = true;
476
477 // Tell serviceability tools these classes are unloading
478 classes_do(InstanceKlass::notify_unload_class);
479
480 LogTarget(Debug, class, loader, data) lt;
481 if (lt.is_enabled()) {
482 ResourceMark rm;
483 LogStream ls(lt);
484 ls.print(": unload loader data " INTPTR_FORMAT, p2i(this));
485 ls.print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)class_loader()),
486 loader_name());
487 if (is_anonymous()) {
488 ls.print(" for anonymous class " INTPTR_FORMAT " ", p2i(_klasses));
489 }
490 ls.cr();
491 }
492
493 // In some rare cases items added to this list will not be freed elsewhere.
494 // To keep it simple, just free everything in it here.
495 free_deallocate_list();
496 }
497
498 ModuleEntryTable* ClassLoaderData::modules() {
499 // Lazily create the module entry table at first request.
500 // Lock-free access requires load_ptr_acquire.
501 ModuleEntryTable* modules = load_ptr_acquire(&_modules);
502 if (modules == NULL) {
503 MutexLocker m1(Module_lock);
504 // Check if _modules got allocated while we were waiting for this lock.
505 if ((modules = _modules) == NULL) {
506 modules = new ModuleEntryTable(ModuleEntryTable::_moduletable_entry_size);
507
508 {
509 MutexLockerEx m1(metaspace_lock(), Mutex::_no_safepoint_check_flag);
510 // Ensure _modules is stable, since it is examined without a lock
788 if (!is_anonymous) {
789 ClassLoaderData** cld_addr = java_lang_ClassLoader::loader_data_addr(loader());
790 // First, Atomically set it
791 ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL);
792 if (old != NULL) {
793 delete cld;
794 // Returns the data.
795 return old;
796 }
797 }
798
799 // We won the race, and therefore the task of adding the data to the list of
800 // class loader data
801 ClassLoaderData** list_head = &_head;
802 ClassLoaderData* next = _head;
803
804 do {
805 cld->set_next(next);
806 ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next);
807 if (exchanged == next) {
808 LogTarget(Debug, class, loader, data) lt;
809 if (lt.is_enabled()) {
810 PauseNoSafepointVerifier pnsv(&no_safepoints); // Need safe points for JavaCalls::call_virtual
811 LogStream ls(lt);
812 print_creation(&ls, loader, cld, CHECK_NULL);
813 }
814 return cld;
815 }
816 next = exchanged;
817 } while (true);
818 }
819
820 void ClassLoaderDataGraph::print_creation(outputStream* out, Handle loader, ClassLoaderData* cld, TRAPS) {
821 Handle string;
822 if (loader.not_null()) {
823 // Include the result of loader.toString() in the output. This allows
824 // the user of the log to identify the class loader instance.
825 JavaValue result(T_OBJECT);
826 Klass* spec_klass = SystemDictionary::ClassLoader_klass();
827 JavaCalls::call_virtual(&result,
828 loader,
829 spec_klass,
830 vmSymbols::toString_name(),
831 vmSymbols::void_string_signature(),
832 CHECK);
833 assert(result.get_type() == T_OBJECT, "just checking");
834 string = Handle(THREAD, (oop)result.get_jobject());
835 }
836
837 ResourceMark rm;
838 out->print("create class loader data " INTPTR_FORMAT, p2i(cld));
839 out->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)cld->class_loader()),
840 cld->loader_name());
841
842 if (string.not_null()) {
843 out->print(": ");
844 java_lang_String::print(string(), out);
845 }
846 out->cr();
847 }
848
849
850 void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
851 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
852 cld->oops_do(f, klass_closure, must_claim);
853 }
854 }
855
856 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
857 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
858 if (cld->keep_alive()) {
859 cld->oops_do(f, klass_closure, must_claim);
860 }
861 }
862 }
863
864 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
865 if (ClassUnloading) {
866 keep_alive_oops_do(f, klass_closure, must_claim);
967 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
968 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
969 // Only walk the head until any clds not purged from prior unloading
970 // (CMS doesn't purge right away).
971 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
972 assert(cld->is_unloading(), "invariant");
973 cld->classes_do(f);
974 }
975 }
976
977 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
978 assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
979
980 GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
981
982 // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
983 ClassLoaderData* curr = _head;
984 while (curr != _saved_head) {
985 if (!curr->claimed()) {
986 array->push(curr);
987 LogTarget(Debug, class, loader, data) lt;
988 if (lt.is_enabled()) {
989 LogStream ls(lt);
990 ls.print("found new CLD: ");
991 curr->print_value_on(&ls);
992 ls.cr();
993 }
994 }
995
996 curr = curr->_next;
997 }
998
999 return array;
1000 }
1001
1002 bool ClassLoaderDataGraph::unload_list_contains(const void* x) {
1003 assert(SafepointSynchronize::is_at_safepoint(), "only safe to call at safepoint");
1004 for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) {
1005 if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {
1006 return true;
1007 }
1008 }
1009 return false;
1010 }
1011
1012 #ifndef PRODUCT
|