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/systemDictionary.hpp"
55 #include "code/codeCache.hpp"
56 #include "memory/gcLocker.hpp"
57 #include "memory/metadataFactory.hpp"
58 #include "memory/metaspaceShared.hpp"
59 #include "memory/oopFactory.hpp"
60 #include "runtime/jniHandles.hpp"
61 #include "runtime/mutex.hpp"
62 #include "runtime/safepoint.hpp"
63 #include "runtime/synchronizer.hpp"
64 #include "utilities/growableArray.hpp"
65 #include "utilities/ostream.hpp"
66
67 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
68
69 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) :
70 _class_loader(h_class_loader()),
71 _is_anonymous(is_anonymous), _keep_alive(is_anonymous), // initially
72 _metaspace(NULL), _unloading(false), _klasses(NULL),
73 _claimed(0), _jmethod_ids(NULL), _handles(NULL), _deallocate_list(NULL),
74 _next(NULL), _dependencies(dependencies),
75 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {
76 // empty
77 }
78
79 void ClassLoaderData::init_dependencies(TRAPS) {
80 assert(!Universe::is_fully_initialized(), "should only be called when initializing");
81 assert(is_the_null_class_loader_data(), "should only call this for the null class loader");
82 _dependencies.init(CHECK);
83 }
84
85 void ClassLoaderData::Dependencies::init(TRAPS) {
86 // Create empty dependencies array to add to. CMS requires this to be
103
104 f->do_oop(&_class_loader);
105 _dependencies.oops_do(f);
106 _handles->oops_do(f);
107 if (klass_closure != NULL) {
108 classes_do(klass_closure);
109 }
110 }
111
112 void ClassLoaderData::Dependencies::oops_do(OopClosure* f) {
113 f->do_oop((oop*)&_list_head);
114 }
115
116 void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
117 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
118 klass_closure->do_klass(k);
119 assert(k != k->next_link(), "no loops!");
120 }
121 }
122
123 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
124 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
125 if (k->oop_is_instance()) {
126 f(InstanceKlass::cast(k));
127 }
128 assert(k != k->next_link(), "no loops!");
129 }
130 }
131
132 void ClassLoaderData::record_dependency(Klass* k, TRAPS) {
133 ClassLoaderData * const from_cld = this;
134 ClassLoaderData * const to_cld = k->class_loader_data();
135
136 // Dependency to the null class loader data doesn't need to be recorded
137 // because the null class loader data never goes away.
138 if (to_cld->is_the_null_class_loader_data()) {
139 return;
140 }
141
142 oop to;
566 }
567 }
568 }
569
570 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
571 if (ClassUnloading) {
572 ClassLoaderData::the_null_class_loader_data()->oops_do(f, klass_closure, must_claim);
573 // keep any special CLDs alive.
574 ClassLoaderDataGraph::keep_alive_oops_do(f, klass_closure, must_claim);
575 } else {
576 ClassLoaderDataGraph::oops_do(f, klass_closure, must_claim);
577 }
578 }
579
580 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) {
581 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
582 cld->classes_do(klass_closure);
583 }
584 }
585
586 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
587 assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
588
589 GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
590
591 // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
592 ClassLoaderData* curr = _head;
593 while (curr != _saved_head) {
594 if (!curr->claimed()) {
595 array->push(curr);
596
597 if (TraceClassLoaderData) {
598 tty->print("[ClassLoaderData] found new CLD: ");
599 curr->print_value_on(tty);
600 tty->cr();
601 }
602 }
603
604 curr = curr->_next;
605 }
670 prev = data;
671 data = data->next();
672 continue;
673 }
674 seen_dead_loader = true;
675 ClassLoaderData* dead = data;
676 dead->unload();
677 data = data->next();
678 // Remove from loader list.
679 // This class loader data will no longer be found
680 // in the ClassLoaderDataGraph.
681 if (prev != NULL) {
682 prev->set_next(data);
683 } else {
684 assert(dead == _head, "sanity check");
685 _head = data;
686 }
687 dead->set_next(_unloading);
688 _unloading = dead;
689 }
690 return seen_dead_loader;
691 }
692
693 void ClassLoaderDataGraph::purge() {
694 ClassLoaderData* list = _unloading;
695 _unloading = NULL;
696 ClassLoaderData* next = list;
697 while (next != NULL) {
698 ClassLoaderData* purge_me = next;
699 next = purge_me->next();
700 delete purge_me;
701 }
702 Metaspace::purge();
703 }
704
705 // CDS support
706
707 // Global metaspaces for writing information to the shared archive. When
708 // application CDS is supported, we may need one per metaspace, so this
709 // sort of looks like it.
710 Metaspace* ClassLoaderData::_ro_metaspace = NULL;
711 Metaspace* ClassLoaderData::_rw_metaspace = NULL;
712 static bool _shared_metaspaces_initialized = false;
713
714 // Initialize shared metaspaces (change to call from somewhere not lazily)
715 void ClassLoaderData::initialize_shared_metaspaces() {
716 assert(DumpSharedSpaces, "only use this for dumping shared spaces");
717 assert(this == ClassLoaderData::the_null_class_loader_data(),
718 "only supported for null loader data for now");
719 assert (!_shared_metaspaces_initialized, "only initialize once");
720 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
721 _ro_metaspace = new Metaspace(_metaspace_lock, Metaspace::ROMetaspaceType);
722 _rw_metaspace = new Metaspace(_metaspace_lock, Metaspace::ReadWriteMetaspaceType);
723 _shared_metaspaces_initialized = true;
724 }
752 data->verify();
753 }
754 }
755
756 void ClassLoaderDataGraph::dump_on(outputStream * const out) {
757 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
758 data->dump(out);
759 }
760 MetaspaceAux::dump(out);
761 }
762 #endif // PRODUCT
763
764 void ClassLoaderData::print_value_on(outputStream* out) const {
765 if (class_loader() == NULL) {
766 out->print("NULL class_loader");
767 } else {
768 out->print("class loader "PTR_FORMAT, this);
769 class_loader()->print_value_on(out);
770 }
771 }
|
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/systemDictionary.hpp"
55 #include "code/codeCache.hpp"
56 #include "memory/gcLocker.hpp"
57 #include "memory/metadataFactory.hpp"
58 #include "memory/metaspaceShared.hpp"
59 #include "memory/oopFactory.hpp"
60 #include "runtime/jniHandles.hpp"
61 #include "runtime/mutex.hpp"
62 #include "runtime/safepoint.hpp"
63 #include "runtime/synchronizer.hpp"
64 #include "utilities/growableArray.hpp"
65 #include "utilities/ostream.hpp"
66
67 #if INCLUDE_TRACE
68 #include "trace/tracing.hpp"
69 #endif
70
71
72 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
73
74 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) :
75 _class_loader(h_class_loader()),
76 _is_anonymous(is_anonymous), _keep_alive(is_anonymous), // initially
77 _metaspace(NULL), _unloading(false), _klasses(NULL),
78 _claimed(0), _jmethod_ids(NULL), _handles(NULL), _deallocate_list(NULL),
79 _next(NULL), _dependencies(dependencies),
80 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {
81 // empty
82 }
83
84 void ClassLoaderData::init_dependencies(TRAPS) {
85 assert(!Universe::is_fully_initialized(), "should only be called when initializing");
86 assert(is_the_null_class_loader_data(), "should only call this for the null class loader");
87 _dependencies.init(CHECK);
88 }
89
90 void ClassLoaderData::Dependencies::init(TRAPS) {
91 // Create empty dependencies array to add to. CMS requires this to be
108
109 f->do_oop(&_class_loader);
110 _dependencies.oops_do(f);
111 _handles->oops_do(f);
112 if (klass_closure != NULL) {
113 classes_do(klass_closure);
114 }
115 }
116
117 void ClassLoaderData::Dependencies::oops_do(OopClosure* f) {
118 f->do_oop((oop*)&_list_head);
119 }
120
121 void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
122 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
123 klass_closure->do_klass(k);
124 assert(k != k->next_link(), "no loops!");
125 }
126 }
127
128 void ClassLoaderData::classes_do(void f(Klass * const)) {
129 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
130 f(k);
131 }
132 }
133
134 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
135 for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
136 if (k->oop_is_instance()) {
137 f(InstanceKlass::cast(k));
138 }
139 assert(k != k->next_link(), "no loops!");
140 }
141 }
142
143 void ClassLoaderData::record_dependency(Klass* k, TRAPS) {
144 ClassLoaderData * const from_cld = this;
145 ClassLoaderData * const to_cld = k->class_loader_data();
146
147 // Dependency to the null class loader data doesn't need to be recorded
148 // because the null class loader data never goes away.
149 if (to_cld->is_the_null_class_loader_data()) {
150 return;
151 }
152
153 oop to;
577 }
578 }
579 }
580
581 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
582 if (ClassUnloading) {
583 ClassLoaderData::the_null_class_loader_data()->oops_do(f, klass_closure, must_claim);
584 // keep any special CLDs alive.
585 ClassLoaderDataGraph::keep_alive_oops_do(f, klass_closure, must_claim);
586 } else {
587 ClassLoaderDataGraph::oops_do(f, klass_closure, must_claim);
588 }
589 }
590
591 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) {
592 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
593 cld->classes_do(klass_closure);
594 }
595 }
596
597 void ClassLoaderDataGraph::classes_do(void f(Klass* const)) {
598 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
599 cld->classes_do(f);
600 }
601 }
602
603 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
604 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
605 for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) {
606 cld->classes_do(f);
607 }
608 }
609
610 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
611 assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
612
613 GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
614
615 // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
616 ClassLoaderData* curr = _head;
617 while (curr != _saved_head) {
618 if (!curr->claimed()) {
619 array->push(curr);
620
621 if (TraceClassLoaderData) {
622 tty->print("[ClassLoaderData] found new CLD: ");
623 curr->print_value_on(tty);
624 tty->cr();
625 }
626 }
627
628 curr = curr->_next;
629 }
694 prev = data;
695 data = data->next();
696 continue;
697 }
698 seen_dead_loader = true;
699 ClassLoaderData* dead = data;
700 dead->unload();
701 data = data->next();
702 // Remove from loader list.
703 // This class loader data will no longer be found
704 // in the ClassLoaderDataGraph.
705 if (prev != NULL) {
706 prev->set_next(data);
707 } else {
708 assert(dead == _head, "sanity check");
709 _head = data;
710 }
711 dead->set_next(_unloading);
712 _unloading = dead;
713 }
714
715 if (seen_dead_loader) {
716 post_class_unload_events();
717 }
718
719 return seen_dead_loader;
720 }
721
722 void ClassLoaderDataGraph::purge() {
723 ClassLoaderData* list = _unloading;
724 _unloading = NULL;
725 ClassLoaderData* next = list;
726 while (next != NULL) {
727 ClassLoaderData* purge_me = next;
728 next = purge_me->next();
729 delete purge_me;
730 }
731 Metaspace::purge();
732 }
733
734 void ClassLoaderDataGraph::post_class_unload_events(void) {
735 #if INCLUDE_TRACE
736 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
737 if (Tracing::enabled()) {
738 if (Tracing::is_event_enabled(TraceClassUnloadEvent)) {
739 assert(_unloading != NULL, "need class loader data unload list!");
740 _class_unload_time = Tracing::time();
741 classes_unloading_do(&class_unload_event);
742 }
743 Tracing::on_unloading_classes();
744 }
745 #endif
746 }
747
748 // CDS support
749
750 // Global metaspaces for writing information to the shared archive. When
751 // application CDS is supported, we may need one per metaspace, so this
752 // sort of looks like it.
753 Metaspace* ClassLoaderData::_ro_metaspace = NULL;
754 Metaspace* ClassLoaderData::_rw_metaspace = NULL;
755 static bool _shared_metaspaces_initialized = false;
756
757 // Initialize shared metaspaces (change to call from somewhere not lazily)
758 void ClassLoaderData::initialize_shared_metaspaces() {
759 assert(DumpSharedSpaces, "only use this for dumping shared spaces");
760 assert(this == ClassLoaderData::the_null_class_loader_data(),
761 "only supported for null loader data for now");
762 assert (!_shared_metaspaces_initialized, "only initialize once");
763 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
764 _ro_metaspace = new Metaspace(_metaspace_lock, Metaspace::ROMetaspaceType);
765 _rw_metaspace = new Metaspace(_metaspace_lock, Metaspace::ReadWriteMetaspaceType);
766 _shared_metaspaces_initialized = true;
767 }
795 data->verify();
796 }
797 }
798
799 void ClassLoaderDataGraph::dump_on(outputStream * const out) {
800 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
801 data->dump(out);
802 }
803 MetaspaceAux::dump(out);
804 }
805 #endif // PRODUCT
806
807 void ClassLoaderData::print_value_on(outputStream* out) const {
808 if (class_loader() == NULL) {
809 out->print("NULL class_loader");
810 } else {
811 out->print("class loader "PTR_FORMAT, this);
812 class_loader()->print_value_on(out);
813 }
814 }
815
816 #if INCLUDE_TRACE
817
818 TracingTime ClassLoaderDataGraph::_class_unload_time;
819
820 void ClassLoaderDataGraph::class_unload_event(Klass* const k) {
821
822 // post class unload event
823 EventClassUnload event(UNTIMED);
824 event.set_endtime(_class_unload_time);
825 event.set_unloadedClass(k);
826 oop defining_class_loader = k->class_loader();
827 event.set_definingClassLoader(defining_class_loader != NULL ?
828 defining_class_loader->klass() : (Klass*)NULL);
829 event.commit();
830 }
831
832 #endif /* INCLUDE_TRACE */
|