97 _claimed(0), _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),
98 _next(NULL), _dependencies(dependencies),
99 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true,
100 Monitor::_safepoint_check_never)) {
101
102 // A ClassLoaderData created solely for an anonymous class should never have a
103 // ModuleEntryTable or PackageEntryTable created for it. The defining package
104 // and module for an anonymous class will be found in its host class.
105 if (!is_anonymous) {
106 _packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size);
107 if (h_class_loader.is_null()) {
108 // Create unnamed module for boot loader
109 _unnamed_module = ModuleEntry::create_boot_unnamed_module(this);
110 } else {
111 // Create unnamed module for all other loaders
112 _unnamed_module = ModuleEntry::create_unnamed_module(this);
113 }
114 } else {
115 _unnamed_module = NULL;
116 }
117 TRACE_INIT_ID(this);
118 }
119
120 void ClassLoaderData::init_dependencies(TRAPS) {
121 assert(!Universe::is_fully_initialized(), "should only be called when initializing");
122 assert(is_the_null_class_loader_data(), "should only call this for the null class loader");
123 _dependencies.init(CHECK);
124 }
125
126 void ClassLoaderData::Dependencies::init(TRAPS) {
127 // Create empty dependencies array to add to. CMS requires this to be
128 // an oop so that it can track additions via card marks. We think.
129 _list_head = oopFactory::new_objectArray(2, CHECK);
130 }
131
132 ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() {
133 Chunk* c = _head;
134 while (c != NULL) {
135 Chunk* next = c->_next;
136 delete c;
436 // Link the new item into the list, making sure the linked class is stable
437 // since the list can be walked without a lock
438 OrderAccess::release_store_ptr(&_klasses, k);
439 }
440
441 if (publicize && k->class_loader_data() != NULL) {
442 ResourceMark rm;
443 log_trace(class, loader, data)("Adding k: " PTR_FORMAT " %s to CLD: "
444 PTR_FORMAT " loader: " PTR_FORMAT " %s",
445 p2i(k),
446 k->external_name(),
447 p2i(k->class_loader_data()),
448 p2i((void *)k->class_loader()),
449 loader_name());
450 }
451 }
452
453 // Class iterator used by the compiler. It gets some number of classes at
454 // a safepoint to decay invocation counters on the methods.
455 class ClassLoaderDataGraphKlassIteratorStatic {
456 ClassLoaderData* _current_loader_data = NULL;
457 Klass* _current_class_entry = NULL;
458 public:
459
460 InstanceKlass* try_get_next_class() {
461 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
462 while (true) {
463
464 if (_current_class_entry != NULL) {
465 Klass* k = _current_class_entry;
466 _current_class_entry = _current_class_entry->next_link();
467
468 if (k->is_instance_klass()) {
469 InstanceKlass* ik = InstanceKlass::cast(k);
470 // Only return loaded classes
471 if (ik->is_loaded()) {
472 return ik;
473 }
474 }
475 } else {
476 // Go to next CLD
477 if (_current_loader_data != NULL) {
478 _current_loader_data = _current_loader_data->next();
479 }
595 // except boot loader and reflection class loaders
596 static int calculate_dictionary_size(int classcount) {
597 static int newsize = 0; // only calculate once
598 if (newsize != 0) {
599 return newsize;
600 }
601 newsize = _primelist[0];
602 if (classcount > 0 && !DumpSharedSpaces) {
603 int index = 0;
604 int desiredsize = classcount/_average_depth_goal;
605 for (newsize = _primelist[index]; index < _prime_array_size -1;
606 newsize = _primelist[++index]) {
607 if (desiredsize <= newsize) {
608 break;
609 }
610 }
611 }
612 return newsize;
613 }
614
615 Dictionary* ClassLoaderData::dictionary_or_null() {
616 return load_ptr_acquire(&_dictionary);
617 }
618
619 Dictionary* ClassLoaderData::dictionary() {
620 assert(!is_anonymous(), "anonymous class loader data do not have a dictionary");
621 // Lazily create the dictionary, in the same way of lazily creating modules.
622 // Lock-free access requires load_ptr_acquire.
623 Dictionary* dictionary = load_ptr_acquire(&_dictionary);
624 if (dictionary == NULL) {
625 MutexLocker m1(SystemDictionary_lock);
626 // Check if _dictionary got allocated while we were waiting for this lock.
627 if ((dictionary = _dictionary) == NULL) {
628 int size;
629 if (this == the_null_class_loader_data()) {
630 size = _boot_loader_dictionary_size;
631 } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
632 size = 1; // there's only one class in relection class loader and no initiated classes
633 } else {
634 size = calculate_dictionary_size(PredictedLoadedClassCount);
635 }
636 dictionary = new Dictionary(this, size);
637 // Ensure _dictionary is stable, since it is examined without a lock.
638 // Don't need metaspace_lock since SystemDictionary_lock is held.
639 OrderAccess::release_store_ptr(&_dictionary, dictionary);
640 }
641 }
642 return dictionary;
643 }
644
645 // Unloading support
646 oop ClassLoaderData::keep_alive_object() const {
647 assert_locked_or_safepoint(_metaspace_lock);
648 assert(!keep_alive(), "Don't use with CLDs that are artificially kept alive");
649 return is_anonymous() ? _klasses->java_mirror() : class_loader();
650 }
651
652 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {
653 bool alive = keep_alive() // null class loader and incomplete anonymous klasses.
654 || is_alive_closure->do_object_b(keep_alive_object());
655
656 return alive;
657 }
658
659 ClassLoaderData::~ClassLoaderData() {
660 // Release C heap structures for all the classes.
661 classes_do(InstanceKlass::release_C_heap_structures);
662
1092 }
1093 }
1094
1095 void ClassLoaderDataGraph::loaded_classes_do(KlassClosure* klass_closure) {
1096 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
1097 cld->loaded_classes_do(klass_closure);
1098 }
1099 }
1100
1101 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
1102 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
1103 // Only walk the head until any clds not purged from prior unloading
1104 // (CMS doesn't purge right away).
1105 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
1106 assert(cld->is_unloading(), "invariant");
1107 cld->classes_do(f);
1108 }
1109 }
1110
1111 #define FOR_ALL_DICTIONARY(X) for (ClassLoaderData* X = _head; X != NULL; X = X->next()) \
1112 if (X->dictionary_or_null() != NULL)
1113
1114 // Walk classes in the loaded class dictionaries in various forms.
1115 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*)) {
1116 FOR_ALL_DICTIONARY(cld) {
1117 cld->dictionary()->classes_do(f);
1118 }
1119 }
1120
1121 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*, TRAPS), TRAPS) {
1122 FOR_ALL_DICTIONARY(cld) {
1123 cld->dictionary()->classes_do(f, CHECK);
1124 }
1125 }
1126
1127 void ClassLoaderDataGraph::dictionary_all_entries_do(void f(InstanceKlass*, ClassLoaderData*)) {
1128 FOR_ALL_DICTIONARY(cld) {
1129 cld->dictionary()->all_entries_do(f);
1130 }
1131 }
1132
1133 void ClassLoaderDataGraph::verify_dictionary() {
1134 FOR_ALL_DICTIONARY(cld) {
1135 cld->dictionary()->verify();
1136 }
1137 }
1138
1139 void ClassLoaderDataGraph::print_dictionary(bool details) {
1140 FOR_ALL_DICTIONARY(cld) {
1141 tty->print("Dictionary for class loader ");
1142 cld->print_value();
1143 tty->cr();
1144 cld->dictionary()->print(details);
1145 }
1146 }
1228 continue;
1229 }
1230 seen_dead_loader = true;
1231 ClassLoaderData* dead = data;
1232 dead->unload();
1233 data = data->next();
1234 // Remove from loader list.
1235 // This class loader data will no longer be found
1236 // in the ClassLoaderDataGraph.
1237 if (prev != NULL) {
1238 prev->set_next(data);
1239 } else {
1240 assert(dead == _head, "sanity check");
1241 _head = data;
1242 }
1243 dead->set_next(_unloading);
1244 _unloading = dead;
1245 }
1246
1247 if (seen_dead_loader) {
1248 // Walk a Dictionary, ModuleEntry's reads, and a PackageEntry's exports
1249 // lists to determine if there are modules on those lists that are now
1250 // dead and should be removed. A module's life cycle is equivalent
1251 // to its defining class loader's life cycle. Since a module is
1252 // considered dead if its class loader is dead, these walks must
1253 // occur after each class loader's aliveness is determined.
1254 data = _head;
1255 while (data != NULL) {
1256 if (data->dictionary_or_null() != NULL) {
1257 data->dictionary()->do_unloading();
1258 }
1259 if (data->packages() != NULL) {
1260 data->packages()->purge_all_package_exports();
1261 }
1262 if (data->modules_defined()) {
1263 data->modules()->purge_all_module_reads();
1264 }
1265 data = data->next();
1266 }
1267
1268 post_class_unload_events();
1269 }
1270
1271 return seen_dead_loader;
1272 }
1273
1274 void ClassLoaderDataGraph::purge() {
1275 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
1276 ClassLoaderData* list = _unloading;
1277 _unloading = NULL;
1278 ClassLoaderData* next = list;
|
97 _claimed(0), _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),
98 _next(NULL), _dependencies(dependencies),
99 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true,
100 Monitor::_safepoint_check_never)) {
101
102 // A ClassLoaderData created solely for an anonymous class should never have a
103 // ModuleEntryTable or PackageEntryTable created for it. The defining package
104 // and module for an anonymous class will be found in its host class.
105 if (!is_anonymous) {
106 _packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size);
107 if (h_class_loader.is_null()) {
108 // Create unnamed module for boot loader
109 _unnamed_module = ModuleEntry::create_boot_unnamed_module(this);
110 } else {
111 // Create unnamed module for all other loaders
112 _unnamed_module = ModuleEntry::create_unnamed_module(this);
113 }
114 } else {
115 _unnamed_module = NULL;
116 }
117
118 if (!is_anonymous) {
119 _dictionary = create_dictionary();
120 } else {
121 _dictionary = NULL;
122 }
123 TRACE_INIT_ID(this);
124 }
125
126 void ClassLoaderData::init_dependencies(TRAPS) {
127 assert(!Universe::is_fully_initialized(), "should only be called when initializing");
128 assert(is_the_null_class_loader_data(), "should only call this for the null class loader");
129 _dependencies.init(CHECK);
130 }
131
132 void ClassLoaderData::Dependencies::init(TRAPS) {
133 // Create empty dependencies array to add to. CMS requires this to be
134 // an oop so that it can track additions via card marks. We think.
135 _list_head = oopFactory::new_objectArray(2, CHECK);
136 }
137
138 ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() {
139 Chunk* c = _head;
140 while (c != NULL) {
141 Chunk* next = c->_next;
142 delete c;
442 // Link the new item into the list, making sure the linked class is stable
443 // since the list can be walked without a lock
444 OrderAccess::release_store_ptr(&_klasses, k);
445 }
446
447 if (publicize && k->class_loader_data() != NULL) {
448 ResourceMark rm;
449 log_trace(class, loader, data)("Adding k: " PTR_FORMAT " %s to CLD: "
450 PTR_FORMAT " loader: " PTR_FORMAT " %s",
451 p2i(k),
452 k->external_name(),
453 p2i(k->class_loader_data()),
454 p2i((void *)k->class_loader()),
455 loader_name());
456 }
457 }
458
459 // Class iterator used by the compiler. It gets some number of classes at
460 // a safepoint to decay invocation counters on the methods.
461 class ClassLoaderDataGraphKlassIteratorStatic {
462 ClassLoaderData* _current_loader_data;
463 Klass* _current_class_entry;
464 public:
465
466 ClassLoaderDataGraphKlassIteratorStatic() : _current_loader_data(NULL), _current_class_entry(NULL) {}
467
468 InstanceKlass* try_get_next_class() {
469 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
470 while (true) {
471
472 if (_current_class_entry != NULL) {
473 Klass* k = _current_class_entry;
474 _current_class_entry = _current_class_entry->next_link();
475
476 if (k->is_instance_klass()) {
477 InstanceKlass* ik = InstanceKlass::cast(k);
478 // Only return loaded classes
479 if (ik->is_loaded()) {
480 return ik;
481 }
482 }
483 } else {
484 // Go to next CLD
485 if (_current_loader_data != NULL) {
486 _current_loader_data = _current_loader_data->next();
487 }
603 // except boot loader and reflection class loaders
604 static int calculate_dictionary_size(int classcount) {
605 static int newsize = 0; // only calculate once
606 if (newsize != 0) {
607 return newsize;
608 }
609 newsize = _primelist[0];
610 if (classcount > 0 && !DumpSharedSpaces) {
611 int index = 0;
612 int desiredsize = classcount/_average_depth_goal;
613 for (newsize = _primelist[index]; index < _prime_array_size -1;
614 newsize = _primelist[++index]) {
615 if (desiredsize <= newsize) {
616 break;
617 }
618 }
619 }
620 return newsize;
621 }
622
623 Dictionary* ClassLoaderData::create_dictionary() {
624 assert(!is_anonymous(), "anonymous class loader data do not have a dictionary");
625 int size;
626 if (_the_null_class_loader_data == NULL) {
627 size = _boot_loader_dictionary_size;
628 } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
629 size = 1; // there's only one class in relection class loader and no initiated classes
630 } else {
631 size = calculate_dictionary_size(PredictedLoadedClassCount);
632 }
633 return new Dictionary(this, size);
634 }
635
636 // Unloading support
637 oop ClassLoaderData::keep_alive_object() const {
638 assert_locked_or_safepoint(_metaspace_lock);
639 assert(!keep_alive(), "Don't use with CLDs that are artificially kept alive");
640 return is_anonymous() ? _klasses->java_mirror() : class_loader();
641 }
642
643 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {
644 bool alive = keep_alive() // null class loader and incomplete anonymous klasses.
645 || is_alive_closure->do_object_b(keep_alive_object());
646
647 return alive;
648 }
649
650 ClassLoaderData::~ClassLoaderData() {
651 // Release C heap structures for all the classes.
652 classes_do(InstanceKlass::release_C_heap_structures);
653
1083 }
1084 }
1085
1086 void ClassLoaderDataGraph::loaded_classes_do(KlassClosure* klass_closure) {
1087 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
1088 cld->loaded_classes_do(klass_closure);
1089 }
1090 }
1091
1092 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
1093 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
1094 // Only walk the head until any clds not purged from prior unloading
1095 // (CMS doesn't purge right away).
1096 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
1097 assert(cld->is_unloading(), "invariant");
1098 cld->classes_do(f);
1099 }
1100 }
1101
1102 #define FOR_ALL_DICTIONARY(X) for (ClassLoaderData* X = _head; X != NULL; X = X->next()) \
1103 if (X->dictionary() != NULL)
1104
1105 // Walk classes in the loaded class dictionaries in various forms.
1106 // Only walks the classes defined in this class loader.
1107 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*)) {
1108 FOR_ALL_DICTIONARY(cld) {
1109 cld->dictionary()->classes_do(f);
1110 }
1111 }
1112
1113 // Only walks the classes defined in this class loader.
1114 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*, TRAPS), TRAPS) {
1115 FOR_ALL_DICTIONARY(cld) {
1116 cld->dictionary()->classes_do(f, CHECK);
1117 }
1118 }
1119
1120 // Walks all entries in the dictionary including entries initiated by this class loader.
1121 void ClassLoaderDataGraph::dictionary_all_entries_do(void f(InstanceKlass*, ClassLoaderData*)) {
1122 FOR_ALL_DICTIONARY(cld) {
1123 cld->dictionary()->all_entries_do(f);
1124 }
1125 }
1126
1127 void ClassLoaderDataGraph::verify_dictionary() {
1128 FOR_ALL_DICTIONARY(cld) {
1129 cld->dictionary()->verify();
1130 }
1131 }
1132
1133 void ClassLoaderDataGraph::print_dictionary(bool details) {
1134 FOR_ALL_DICTIONARY(cld) {
1135 tty->print("Dictionary for class loader ");
1136 cld->print_value();
1137 tty->cr();
1138 cld->dictionary()->print(details);
1139 }
1140 }
1222 continue;
1223 }
1224 seen_dead_loader = true;
1225 ClassLoaderData* dead = data;
1226 dead->unload();
1227 data = data->next();
1228 // Remove from loader list.
1229 // This class loader data will no longer be found
1230 // in the ClassLoaderDataGraph.
1231 if (prev != NULL) {
1232 prev->set_next(data);
1233 } else {
1234 assert(dead == _head, "sanity check");
1235 _head = data;
1236 }
1237 dead->set_next(_unloading);
1238 _unloading = dead;
1239 }
1240
1241 if (seen_dead_loader) {
1242 data = _head;
1243 while (data != NULL) {
1244 // Remove entries in the dictionary of live class loader that have
1245 // initiated loading classes in a dead class loader.
1246 if (data->dictionary() != NULL) {
1247 data->dictionary()->do_unloading();
1248 }
1249 // Walk a ModuleEntry's reads, and a PackageEntry's exports
1250 // lists to determine if there are modules on those lists that are now
1251 // dead and should be removed. A module's life cycle is equivalent
1252 // to its defining class loader's life cycle. Since a module is
1253 // considered dead if its class loader is dead, these walks must
1254 // occur after each class loader's aliveness is determined.
1255 if (data->packages() != NULL) {
1256 data->packages()->purge_all_package_exports();
1257 }
1258 if (data->modules_defined()) {
1259 data->modules()->purge_all_module_reads();
1260 }
1261 data = data->next();
1262 }
1263
1264 post_class_unload_events();
1265 }
1266
1267 return seen_dead_loader;
1268 }
1269
1270 void ClassLoaderDataGraph::purge() {
1271 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
1272 ClassLoaderData* list = _unloading;
1273 _unloading = NULL;
1274 ClassLoaderData* next = list;
|