37 // ClassLoaderData carries information related to a linkset (e.g.,
38 // metaspace holding its klass definitions).
39 // The System Dictionary and related data structures (e.g., placeholder table,
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/dictionary.hpp"
53 #include "classfile/javaClasses.hpp"
54 #include "classfile/metadataOnStackMark.hpp"
55 #include "classfile/moduleEntry.hpp"
56 #include "classfile/packageEntry.hpp"
57 #include "classfile/systemDictionary.hpp"
58 #include "logging/log.hpp"
59 #include "logging/logStream.hpp"
60 #include "memory/allocation.inline.hpp"
61 #include "memory/metadataFactory.hpp"
62 #include "memory/metaspaceShared.hpp"
63 #include "memory/resourceArea.hpp"
64 #include "memory/universe.hpp"
65 #include "oops/access.inline.hpp"
66 #include "oops/oop.inline.hpp"
67 #include "oops/weakHandle.inline.hpp"
68 #include "runtime/atomic.hpp"
69 #include "runtime/handles.inline.hpp"
70 #include "runtime/mutex.hpp"
71 #include "runtime/orderAccess.hpp"
72 #include "runtime/safepoint.hpp"
73 #include "runtime/safepointVerifiers.hpp"
74 #include "utilities/growableArray.hpp"
75 #include "utilities/macros.hpp"
76 #include "utilities/ostream.hpp"
84 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
85
86 void ClassLoaderData::init_null_class_loader_data() {
87 assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
88 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
89
90 _the_null_class_loader_data = new ClassLoaderData(Handle(), false);
91 ClassLoaderDataGraph::_head = _the_null_class_loader_data;
92 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
93
94 LogTarget(Debug, class, loader, data) lt;
95 if (lt.is_enabled()) {
96 ResourceMark rm;
97 LogStream ls(lt);
98 ls.print("create ");
99 _the_null_class_loader_data->print_value_on(&ls);
100 ls.cr();
101 }
102 }
103
104 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous) :
105 _class_loader(h_class_loader()),
106 _is_anonymous(is_anonymous),
107 // An anonymous class loader data doesn't have anything to keep
108 // it from being unloaded during parsing of the anonymous class.
109 // The null-class-loader should always be kept alive.
110 _keep_alive((is_anonymous || h_class_loader.is_null()) ? 1 : 0),
111 _metaspace(NULL), _unloading(false), _klasses(NULL),
112 _modules(NULL), _packages(NULL), _unnamed_module(NULL), _dictionary(NULL),
113 _claimed(0), _modified_oops(true), _accumulated_modified_oops(false),
114 _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),
115 _next(NULL),
116 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true,
117 Monitor::_safepoint_check_never)) {
118
119 if (!is_anonymous) {
120 // The holder is initialized later for anonymous classes, and before calling anything
121 // that call class_loader().
122 initialize_holder(h_class_loader);
123
124 // A ClassLoaderData created solely for an anonymous class should never have a
125 // ModuleEntryTable or PackageEntryTable created for it. The defining package
126 // and module for an anonymous class will be found in its host class.
127 _packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size);
128 if (h_class_loader.is_null()) {
129 // Create unnamed module for boot loader
130 _unnamed_module = ModuleEntry::create_boot_unnamed_module(this);
131 } else {
132 // Create unnamed module for all other loaders
133 _unnamed_module = ModuleEntry::create_unnamed_module(this);
134 }
135 _dictionary = create_dictionary();
136 }
137
138 NOT_PRODUCT(_dependency_count = 0); // number of class loader dependencies
251 }
252 }
253
254 void ClassLoaderData::dec_keep_alive() {
255 if (is_anonymous()) {
256 assert(_keep_alive > 0, "Invalid keep alive decrement count");
257 _keep_alive--;
258 }
259 }
260
261 void ClassLoaderData::oops_do(OopClosure* f, bool must_claim, bool clear_mod_oops) {
262 if (must_claim && !claim()) {
263 return;
264 }
265
266 // Only clear modified_oops after the ClassLoaderData is claimed.
267 if (clear_mod_oops) {
268 clear_modified_oops();
269 }
270
271 f->do_oop(&_class_loader);
272 _handles.oops_do(f);
273 }
274
275 void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
276 // Lock-free access requires load_acquire
277 for (Klass* k = OrderAccess::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
278 klass_closure->do_klass(k);
279 assert(k != k->next_link(), "no loops!");
280 }
281 }
282
283 void ClassLoaderData::classes_do(void f(Klass * const)) {
284 // Lock-free access requires load_acquire
285 for (Klass* k = OrderAccess::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
286 f(k);
287 assert(k != k->next_link(), "no loops!");
288 }
289 }
290
291 void ClassLoaderData::methods_do(void f(Method*)) {
538 }
539
540 return;
541 }
542 prev = k;
543 assert(k != k->next_link(), "no loops!");
544 }
545 ShouldNotReachHere(); // should have found this class!!
546 }
547
548 void ClassLoaderData::unload() {
549 _unloading = true;
550
551 // Tell serviceability tools these classes are unloading
552 classes_do(InstanceKlass::notify_unload_class);
553
554 LogTarget(Debug, class, loader, data) lt;
555 if (lt.is_enabled()) {
556 ResourceMark rm;
557 LogStream ls(lt);
558 ls.print("unload ");
559 print_value_on(&ls);
560 ls.cr();
561 }
562
563 // Some items on the _deallocate_list need to free their C heap structures
564 // if they are not already on the _klasses list.
565 unload_deallocate_list();
566
567 // Clean up global class iterator for compiler
568 static_klass_iterator.adjust_saved_class(this);
569 }
570
571 ModuleEntryTable* ClassLoaderData::modules() {
572 // Lazily create the module entry table at first request.
573 // Lock-free access requires load_acquire.
574 ModuleEntryTable* modules = OrderAccess::load_acquire(&_modules);
575 if (modules == NULL) {
576 MutexLocker m1(Module_lock);
577 // Check if _modules got allocated while we were waiting for this lock.
578 if ((modules = _modules) == NULL) {
613 return new Dictionary(this, size, resizable);
614 }
615
616 // Tell the GC to keep this klass alive while iterating ClassLoaderDataGraph
617 oop ClassLoaderData::holder_phantom() const {
618 // A klass that was previously considered dead can be looked up in the
619 // CLD/SD, and its _java_mirror or _class_loader can be stored in a root
620 // or a reachable object making it alive again. The SATB part of G1 needs
621 // to get notified about this potential resurrection, otherwise the marking
622 // might not find the object.
623 if (!_holder.is_null()) { // NULL class_loader
624 return _holder.resolve();
625 } else {
626 return NULL;
627 }
628 }
629
630 // Unloading support
631 bool ClassLoaderData::is_alive() const {
632 bool alive = keep_alive() // null class loader and incomplete anonymous klasses.
633 || (_holder.peek() != NULL); // not cleaned by weak reference processing
634
635 return alive;
636 }
637
638 class ReleaseKlassClosure: public KlassClosure {
639 private:
640 size_t _instance_class_released;
641 size_t _array_class_released;
642 public:
643 ReleaseKlassClosure() : _instance_class_released(0), _array_class_released(0) { }
644
645 size_t instance_class_released() const { return _instance_class_released; }
646 size_t array_class_released() const { return _array_class_released; }
647
648 void do_klass(Klass* k) {
649 if (k->is_array_klass()) {
650 _array_class_released ++;
651 } else {
652 assert(k->is_instance_klass(), "Must be");
653 _instance_class_released ++;
869 if (m->is_constantPool()) {
870 ((ConstantPool*)m)->release_C_heap_structures();
871 } else if (m->is_klass()) {
872 InstanceKlass* ik = (InstanceKlass*)m;
873 // also releases ik->constants() C heap memory
874 InstanceKlass::release_C_heap_structures(ik);
875 // Remove the class so unloading events aren't triggered for
876 // this class (scratch or error class) in do_unloading().
877 remove_class(ik);
878 }
879 }
880 }
881
882 // These anonymous class loaders are to contain classes used for JSR292
883 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(Handle loader) {
884 // Add a new class loader data to the graph.
885 return ClassLoaderDataGraph::add(loader, true);
886 }
887
888 const char* ClassLoaderData::loader_name() const {
889 // Handles null class loader
890 return SystemDictionary::loader_name(class_loader());
891 }
892
893
894 void ClassLoaderData::print_value_on(outputStream* out) const {
895 if (class_loader() != NULL) {
896 out->print("loader data: " INTPTR_FORMAT " for instance ", p2i(this));
897 class_loader()->print_value_on(out); // includes loader_name() and address of class loader instance
898 } else {
899 // loader data: 0xsomeaddr of <bootloader>
900 out->print("loader data: " INTPTR_FORMAT " of %s", p2i(this), loader_name());
901 }
902 if (is_anonymous()) {
903 out->print(" anonymous");
904 }
905 }
906
907 #ifndef PRODUCT
908 void ClassLoaderData::print_on(outputStream* out) const {
909 out->print("ClassLoaderData CLD: " PTR_FORMAT ", loader: " PTR_FORMAT ", loader_klass: %s {",
910 p2i(this), p2i((void *)class_loader()), loader_name());
911 if (is_anonymous()) out->print(" anonymous");
912 if (claimed()) out->print(" claimed");
913 if (is_unloading()) out->print(" unloading");
914 out->print(" metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null()));
915
916 if (_jmethod_ids != NULL) {
917 Method::print_jmethod_ids(this, out);
918 }
919 out->print(" handles count %d", _handles.count());
920 out->print(" dependencies %d", _dependency_count);
921 out->print_cr("}");
922 }
923 #endif // PRODUCT
924
925 void ClassLoaderData::verify() {
926 assert_locked_or_safepoint(_metaspace_lock);
927 oop cl = class_loader();
928
929 guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same");
930 guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be");
944 bool ClassLoaderData::contains_klass(Klass* klass) {
945 // Lock-free access requires load_acquire
946 for (Klass* k = OrderAccess::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
947 if (k == klass) return true;
948 }
949 return false;
950 }
951
952
953 // GC root of class loader data created.
954 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
955 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
956 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
957 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
958
959 bool ClassLoaderDataGraph::_should_purge = false;
960 bool ClassLoaderDataGraph::_metaspace_oom = false;
961
962 // Add a new class loader data node to the list. Assign the newly created
963 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
964 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous) {
965 NoSafepointVerifier no_safepoints; // we mustn't GC until we've installed the
966 // ClassLoaderData in the graph since the CLD
967 // contains unhandled oops
968
969 ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous);
970
971 if (!is_anonymous) {
972 // First, Atomically set it
973 ClassLoaderData* old = java_lang_ClassLoader::cmpxchg_loader_data(cld, loader(), NULL);
974 if (old != NULL) {
975 delete cld;
976 // Returns the data.
977 return old;
978 }
979 }
980
981 // We won the race, and therefore the task of adding the data to the list of
982 // class loader data
983 ClassLoaderData** list_head = &_head;
984 ClassLoaderData* next = _head;
985
986 do {
987 cld->set_next(next);
988 ClassLoaderData* exchanged = Atomic::cmpxchg(cld, list_head, next);
989 if (exchanged == next) {
990 LogTarget(Debug, class, loader, data) lt;
991 if (lt.is_enabled()) {
992 ResourceMark rm;
993 LogStream ls(lt);
994 ls.print("create ");
995 cld->print_value_on(&ls);
996 ls.cr();
997 }
998 return cld;
999 }
1000 next = exchanged;
1001 } while (true);
1002 }
1003
1004 void ClassLoaderDataGraph::oops_do(OopClosure* f, bool must_claim) {
1005 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
1006 cld->oops_do(f, must_claim);
1007 }
1008 }
1009
1010 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, bool must_claim) {
1011 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
1012 if (cld->keep_alive()) {
1013 cld->oops_do(f, must_claim);
1014 }
1015 }
1016 }
1017
1018 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, bool must_claim) {
1019 if (ClassUnloading) {
1020 keep_alive_oops_do(f, must_claim);
1021 } else {
|
37 // ClassLoaderData carries information related to a linkset (e.g.,
38 // metaspace holding its klass definitions).
39 // The System Dictionary and related data structures (e.g., placeholder table,
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/dictionary.hpp"
53 #include "classfile/javaClasses.hpp"
54 #include "classfile/metadataOnStackMark.hpp"
55 #include "classfile/moduleEntry.hpp"
56 #include "classfile/packageEntry.hpp"
57 #include "classfile/symbolTable.hpp"
58 #include "classfile/systemDictionary.hpp"
59 #include "logging/log.hpp"
60 #include "logging/logStream.hpp"
61 #include "memory/allocation.inline.hpp"
62 #include "memory/metadataFactory.hpp"
63 #include "memory/metaspaceShared.hpp"
64 #include "memory/resourceArea.hpp"
65 #include "memory/universe.hpp"
66 #include "oops/access.inline.hpp"
67 #include "oops/oop.inline.hpp"
68 #include "oops/weakHandle.inline.hpp"
69 #include "runtime/atomic.hpp"
70 #include "runtime/handles.inline.hpp"
71 #include "runtime/mutex.hpp"
72 #include "runtime/orderAccess.hpp"
73 #include "runtime/safepoint.hpp"
74 #include "runtime/safepointVerifiers.hpp"
75 #include "utilities/growableArray.hpp"
76 #include "utilities/macros.hpp"
77 #include "utilities/ostream.hpp"
85 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
86
87 void ClassLoaderData::init_null_class_loader_data() {
88 assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
89 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
90
91 _the_null_class_loader_data = new ClassLoaderData(Handle(), false);
92 ClassLoaderDataGraph::_head = _the_null_class_loader_data;
93 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
94
95 LogTarget(Debug, class, loader, data) lt;
96 if (lt.is_enabled()) {
97 ResourceMark rm;
98 LogStream ls(lt);
99 ls.print("create ");
100 _the_null_class_loader_data->print_value_on(&ls);
101 ls.cr();
102 }
103 }
104
105 // JFR and logging support so that the name and klass are available after the
106 // class_loader oop is no longer alive, during unloading.
107 void ClassLoaderData::initialize_name_and_klass(Handle class_loader) {
108 _class_loader_klass = class_loader->klass();
109 oop class_loader_name = java_lang_ClassLoader::name(class_loader());
110 if (class_loader_name != NULL) {
111 Thread* THREAD = Thread::current();
112 ResourceMark rm(THREAD);
113 const char* class_loader_instance_name =
114 java_lang_String::as_utf8_string(class_loader_name);
115
116 if (class_loader_instance_name != NULL && class_loader_instance_name[0] != '\0') {
117 // Can't throw InternalError and SymbolTable doesn't throw OOM anymore.
118 _class_loader_name = SymbolTable::new_symbol(class_loader_instance_name, CATCH);
119 }
120 }
121 }
122
123 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous) :
124 _is_anonymous(is_anonymous),
125 // An anonymous class loader data doesn't have anything to keep
126 // it from being unloaded during parsing of the anonymous class.
127 // The null-class-loader should always be kept alive.
128 _keep_alive((is_anonymous || h_class_loader.is_null()) ? 1 : 0),
129 _metaspace(NULL), _unloading(false), _klasses(NULL),
130 _modules(NULL), _packages(NULL), _unnamed_module(NULL), _dictionary(NULL),
131 _claimed(0), _modified_oops(true), _accumulated_modified_oops(false),
132 _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),
133 _next(NULL),
134 _class_loader_klass(NULL), _class_loader_name(NULL),
135 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true,
136 Monitor::_safepoint_check_never)) {
137
138 if (!h_class_loader.is_null()) {
139 _class_loader = _handles.add(h_class_loader());
140 }
141
142 if (!is_anonymous) {
143 // The holder is initialized later for anonymous classes, and before calling anything
144 // that call class_loader().
145 initialize_holder(h_class_loader);
146
147 // A ClassLoaderData created solely for an anonymous class should never have a
148 // ModuleEntryTable or PackageEntryTable created for it. The defining package
149 // and module for an anonymous class will be found in its host class.
150 _packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size);
151 if (h_class_loader.is_null()) {
152 // Create unnamed module for boot loader
153 _unnamed_module = ModuleEntry::create_boot_unnamed_module(this);
154 } else {
155 // Create unnamed module for all other loaders
156 _unnamed_module = ModuleEntry::create_unnamed_module(this);
157 }
158 _dictionary = create_dictionary();
159 }
160
161 NOT_PRODUCT(_dependency_count = 0); // number of class loader dependencies
274 }
275 }
276
277 void ClassLoaderData::dec_keep_alive() {
278 if (is_anonymous()) {
279 assert(_keep_alive > 0, "Invalid keep alive decrement count");
280 _keep_alive--;
281 }
282 }
283
284 void ClassLoaderData::oops_do(OopClosure* f, bool must_claim, bool clear_mod_oops) {
285 if (must_claim && !claim()) {
286 return;
287 }
288
289 // Only clear modified_oops after the ClassLoaderData is claimed.
290 if (clear_mod_oops) {
291 clear_modified_oops();
292 }
293
294 _handles.oops_do(f);
295 }
296
297 void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
298 // Lock-free access requires load_acquire
299 for (Klass* k = OrderAccess::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
300 klass_closure->do_klass(k);
301 assert(k != k->next_link(), "no loops!");
302 }
303 }
304
305 void ClassLoaderData::classes_do(void f(Klass * const)) {
306 // Lock-free access requires load_acquire
307 for (Klass* k = OrderAccess::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
308 f(k);
309 assert(k != k->next_link(), "no loops!");
310 }
311 }
312
313 void ClassLoaderData::methods_do(void f(Method*)) {
560 }
561
562 return;
563 }
564 prev = k;
565 assert(k != k->next_link(), "no loops!");
566 }
567 ShouldNotReachHere(); // should have found this class!!
568 }
569
570 void ClassLoaderData::unload() {
571 _unloading = true;
572
573 // Tell serviceability tools these classes are unloading
574 classes_do(InstanceKlass::notify_unload_class);
575
576 LogTarget(Debug, class, loader, data) lt;
577 if (lt.is_enabled()) {
578 ResourceMark rm;
579 LogStream ls(lt);
580 ls.print("unload");
581 print_value_on(&ls);
582 ls.cr();
583 }
584
585 // Some items on the _deallocate_list need to free their C heap structures
586 // if they are not already on the _klasses list.
587 unload_deallocate_list();
588
589 // Clean up global class iterator for compiler
590 static_klass_iterator.adjust_saved_class(this);
591 }
592
593 ModuleEntryTable* ClassLoaderData::modules() {
594 // Lazily create the module entry table at first request.
595 // Lock-free access requires load_acquire.
596 ModuleEntryTable* modules = OrderAccess::load_acquire(&_modules);
597 if (modules == NULL) {
598 MutexLocker m1(Module_lock);
599 // Check if _modules got allocated while we were waiting for this lock.
600 if ((modules = _modules) == NULL) {
635 return new Dictionary(this, size, resizable);
636 }
637
638 // Tell the GC to keep this klass alive while iterating ClassLoaderDataGraph
639 oop ClassLoaderData::holder_phantom() const {
640 // A klass that was previously considered dead can be looked up in the
641 // CLD/SD, and its _java_mirror or _class_loader can be stored in a root
642 // or a reachable object making it alive again. The SATB part of G1 needs
643 // to get notified about this potential resurrection, otherwise the marking
644 // might not find the object.
645 if (!_holder.is_null()) { // NULL class_loader
646 return _holder.resolve();
647 } else {
648 return NULL;
649 }
650 }
651
652 // Unloading support
653 bool ClassLoaderData::is_alive() const {
654 bool alive = keep_alive() // null class loader and incomplete anonymous klasses.
655 || (_holder.peek() != NULL); // and not cleaned by the GC weak handle processing.
656
657 return alive;
658 }
659
660 class ReleaseKlassClosure: public KlassClosure {
661 private:
662 size_t _instance_class_released;
663 size_t _array_class_released;
664 public:
665 ReleaseKlassClosure() : _instance_class_released(0), _array_class_released(0) { }
666
667 size_t instance_class_released() const { return _instance_class_released; }
668 size_t array_class_released() const { return _array_class_released; }
669
670 void do_klass(Klass* k) {
671 if (k->is_array_klass()) {
672 _array_class_released ++;
673 } else {
674 assert(k->is_instance_klass(), "Must be");
675 _instance_class_released ++;
891 if (m->is_constantPool()) {
892 ((ConstantPool*)m)->release_C_heap_structures();
893 } else if (m->is_klass()) {
894 InstanceKlass* ik = (InstanceKlass*)m;
895 // also releases ik->constants() C heap memory
896 InstanceKlass::release_C_heap_structures(ik);
897 // Remove the class so unloading events aren't triggered for
898 // this class (scratch or error class) in do_unloading().
899 remove_class(ik);
900 }
901 }
902 }
903
904 // These anonymous class loaders are to contain classes used for JSR292
905 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(Handle loader) {
906 // Add a new class loader data to the graph.
907 return ClassLoaderDataGraph::add(loader, true);
908 }
909
910 const char* ClassLoaderData::loader_name() const {
911 if (is_unloading()) {
912 if (_class_loader_klass == NULL) {
913 return "<bootloader>";
914 } else if (_class_loader_name != NULL) {
915 return _class_loader_name->as_C_string();
916 } else {
917 return _class_loader_klass->name()->as_C_string();
918 }
919 } else {
920 // Handles null class loader
921 return SystemDictionary::loader_name(class_loader());
922 }
923 }
924
925
926 void ClassLoaderData::print_value_on(outputStream* out) const {
927 if (!is_unloading() && class_loader() != NULL) {
928 out->print("loader data: " INTPTR_FORMAT " for instance ", p2i(this));
929 class_loader()->print_value_on(out); // includes loader_name() and address of class loader instance
930 } else {
931 // loader data: 0xsomeaddr of <bootloader>
932 out->print("loader data: " INTPTR_FORMAT " of %s", p2i(this), loader_name());
933 }
934 if (is_anonymous()) {
935 out->print(" anonymous");
936 }
937 }
938
939 #ifndef PRODUCT
940 void ClassLoaderData::print_on(outputStream* out) const {
941 out->print("ClassLoaderData CLD: " PTR_FORMAT ", loader: " PTR_FORMAT ", loader_klass: %s {",
942 p2i(this), p2i((void *)_class_loader.ptr_raw()), loader_name());
943 if (is_anonymous()) out->print(" anonymous");
944 if (claimed()) out->print(" claimed");
945 if (is_unloading()) out->print(" unloading");
946 out->print(" metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null()));
947
948 if (_jmethod_ids != NULL) {
949 Method::print_jmethod_ids(this, out);
950 }
951 out->print(" handles count %d", _handles.count());
952 out->print(" dependencies %d", _dependency_count);
953 out->print_cr("}");
954 }
955 #endif // PRODUCT
956
957 void ClassLoaderData::verify() {
958 assert_locked_or_safepoint(_metaspace_lock);
959 oop cl = class_loader();
960
961 guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same");
962 guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be");
976 bool ClassLoaderData::contains_klass(Klass* klass) {
977 // Lock-free access requires load_acquire
978 for (Klass* k = OrderAccess::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
979 if (k == klass) return true;
980 }
981 return false;
982 }
983
984
985 // GC root of class loader data created.
986 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
987 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
988 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
989 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
990
991 bool ClassLoaderDataGraph::_should_purge = false;
992 bool ClassLoaderDataGraph::_metaspace_oom = false;
993
994 // Add a new class loader data node to the list. Assign the newly created
995 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
996 ClassLoaderData* ClassLoaderDataGraph::add_to_graph(Handle loader, bool is_anonymous) {
997 NoSafepointVerifier no_safepoints; // we mustn't GC until we've installed the
998 // ClassLoaderData in the graph since the CLD
999 // contains oops in _handles that must be walked.
1000
1001 ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous);
1002
1003 if (!is_anonymous) {
1004 // First, Atomically set it
1005 ClassLoaderData* old = java_lang_ClassLoader::cmpxchg_loader_data(cld, loader(), NULL);
1006 if (old != NULL) {
1007 delete cld;
1008 // Returns the data.
1009 return old;
1010 }
1011 }
1012
1013 // We won the race, and therefore the task of adding the data to the list of
1014 // class loader data
1015 ClassLoaderData** list_head = &_head;
1016 ClassLoaderData* next = _head;
1017
1018 do {
1019 cld->set_next(next);
1020 ClassLoaderData* exchanged = Atomic::cmpxchg(cld, list_head, next);
1021 if (exchanged == next) {
1022 LogTarget(Debug, class, loader, data) lt;
1023 if (lt.is_enabled()) {
1024 ResourceMark rm;
1025 LogStream ls(lt);
1026 ls.print("create ");
1027 cld->print_value_on(&ls);
1028 ls.cr();
1029 }
1030 return cld;
1031 }
1032 next = exchanged;
1033 } while (true);
1034 }
1035
1036 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous) {
1037 ClassLoaderData* loader_data = add_to_graph(loader, is_anonymous);
1038 // Initialize name and class after the loader data is added to the CLDG
1039 // because adding the Symbol for the name might safepoint.
1040 if (loader.not_null()) {
1041 loader_data->initialize_name_and_klass(loader);
1042 }
1043 return loader_data;
1044 }
1045
1046 void ClassLoaderDataGraph::oops_do(OopClosure* f, bool must_claim) {
1047 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
1048 cld->oops_do(f, must_claim);
1049 }
1050 }
1051
1052 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, bool must_claim) {
1053 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
1054 if (cld->keep_alive()) {
1055 cld->oops_do(f, must_claim);
1056 }
1057 }
1058 }
1059
1060 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, bool must_claim) {
1061 if (ClassUnloading) {
1062 keep_alive_oops_do(f, must_claim);
1063 } else {
|