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/oopHandle.inline.hpp" 69 #include "oops/weakHandle.inline.hpp" 70 #include "runtime/atomic.hpp" 71 #include "runtime/handles.inline.hpp" 72 #include "runtime/mutex.hpp" 73 #include "runtime/orderAccess.hpp" 74 #include "runtime/safepoint.hpp" 75 #include "runtime/safepointVerifiers.hpp" 76 #include "utilities/growableArray.hpp" 77 #include "utilities/macros.hpp" 78 #include "utilities/ostream.hpp" 79 #include "utilities/ticks.hpp" 80 #if INCLUDE_JFR 81 #include "jfr/jfr.hpp" 82 #include "jfr/jfrEvents.hpp" 83 #endif 84 85 volatile size_t ClassLoaderDataGraph::_num_array_classes = 0; 86 volatile size_t ClassLoaderDataGraph::_num_instance_classes = 0; 87 88 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL; 89 90 void ClassLoaderData::init_null_class_loader_data() { 91 assert(_the_null_class_loader_data == NULL, "cannot initialize twice"); 92 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice"); 93 94 _the_null_class_loader_data = new ClassLoaderData(Handle(), false); 95 ClassLoaderDataGraph::_head = _the_null_class_loader_data; 96 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be"); 97 98 LogTarget(Trace, class, loader, data) lt; 99 if (lt.is_enabled()) { 100 ResourceMark rm; 101 LogStream ls(lt); 102 ls.print("create "); 103 _the_null_class_loader_data->print_value_on(&ls); 1299 } 1300 1301 curr = curr->_next; 1302 } 1303 1304 return array; 1305 } 1306 1307 #ifndef PRODUCT 1308 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) { 1309 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) { 1310 if (loader_data == data) { 1311 return true; 1312 } 1313 } 1314 1315 return false; 1316 } 1317 #endif // PRODUCT 1318 1319 #if INCLUDE_JFR 1320 static Ticks class_unload_time; 1321 static void post_class_unload_event(Klass* const k) { 1322 assert(k != NULL, "invariant"); 1323 EventClassUnload event(UNTIMED); 1324 event.set_endtime(class_unload_time); 1325 event.set_unloadedClass(k); 1326 event.set_definingClassLoader(k->class_loader_data()); 1327 event.commit(); 1328 } 1329 1330 static void post_class_unload_events() { 1331 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); 1332 if (Jfr::is_enabled()) { 1333 if (EventClassUnload::is_enabled()) { 1334 class_unload_time = Ticks::now(); 1335 ClassLoaderDataGraph::classes_unloading_do(&post_class_unload_event); 1336 } 1337 Jfr::on_unloading_classes(); 1338 } 1339 } 1340 #endif // INCLUDE_JFR 1341 1342 // Move class loader data from main list to the unloaded list for unloading 1343 // and deallocation later. 1344 bool ClassLoaderDataGraph::do_unloading(bool do_cleaning) { 1345 1346 // Indicate whether safepoint cleanup is needed. 1347 _safepoint_cleanup_needed |= do_cleaning; 1348 1349 ClassLoaderData* data = _head; 1350 ClassLoaderData* prev = NULL; 1351 bool seen_dead_loader = false; 1352 uint loaders_processed = 0; 1353 uint loaders_removed = 0; 1354 1355 // Save previous _unloading pointer for CMS which may add to unloading list before 1356 // purging and we don't want to rewalk the previously unloaded class loader data. 1357 _saved_unloading = _unloading; 1358 1359 data = _head; 1360 while (data != NULL) { 1361 if (data->is_alive()) { 1363 data = data->next(); 1364 loaders_processed++; 1365 continue; 1366 } 1367 seen_dead_loader = true; 1368 loaders_removed++; 1369 ClassLoaderData* dead = data; 1370 dead->unload(); 1371 data = data->next(); 1372 // Remove from loader list. 1373 // This class loader data will no longer be found 1374 // in the ClassLoaderDataGraph. 1375 if (prev != NULL) { 1376 prev->set_next(data); 1377 } else { 1378 assert(dead == _head, "sanity check"); 1379 _head = data; 1380 } 1381 dead->set_next(_unloading); 1382 _unloading = dead; 1383 } 1384 1385 if (seen_dead_loader) { 1386 JFR_ONLY(post_class_unload_events();) 1387 } 1388 1389 log_debug(class, loader, data)("do_unloading: loaders processed %u, loaders removed %u", loaders_processed, loaders_removed); 1390 1391 return seen_dead_loader; 1392 } 1393 1394 // There's at least one dead class loader. Purge refererences of healthy module 1395 // reads lists and package export lists to modules belonging to dead loaders. 1396 void ClassLoaderDataGraph::clean_module_and_package_info() { 1397 ClassLoaderData* data = _head; 1398 while (data != NULL) { 1399 // Remove entries in the dictionary of live class loader that have 1400 // initiated loading classes in a dead class loader. 1401 if (data->dictionary() != NULL) { 1402 data->dictionary()->do_unloading(); 1403 } 1404 // Walk a ModuleEntry's reads, and a PackageEntry's exports 1405 // lists to determine if there are modules on those lists that are now 1406 // dead and should be removed. A module's life cycle is equivalent | 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/oopHandle.inline.hpp" 69 #include "oops/weakHandle.inline.hpp" 70 #include "runtime/atomic.hpp" 71 #include "runtime/handles.inline.hpp" 72 #include "runtime/mutex.hpp" 73 #include "runtime/orderAccess.hpp" 74 #include "runtime/safepoint.hpp" 75 #include "runtime/safepointVerifiers.hpp" 76 #include "utilities/growableArray.hpp" 77 #include "utilities/macros.hpp" 78 #include "utilities/ostream.hpp" 79 80 volatile size_t ClassLoaderDataGraph::_num_array_classes = 0; 81 volatile size_t ClassLoaderDataGraph::_num_instance_classes = 0; 82 83 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL; 84 85 void ClassLoaderData::init_null_class_loader_data() { 86 assert(_the_null_class_loader_data == NULL, "cannot initialize twice"); 87 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice"); 88 89 _the_null_class_loader_data = new ClassLoaderData(Handle(), false); 90 ClassLoaderDataGraph::_head = _the_null_class_loader_data; 91 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be"); 92 93 LogTarget(Trace, class, loader, data) lt; 94 if (lt.is_enabled()) { 95 ResourceMark rm; 96 LogStream ls(lt); 97 ls.print("create "); 98 _the_null_class_loader_data->print_value_on(&ls); 1294 } 1295 1296 curr = curr->_next; 1297 } 1298 1299 return array; 1300 } 1301 1302 #ifndef PRODUCT 1303 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) { 1304 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) { 1305 if (loader_data == data) { 1306 return true; 1307 } 1308 } 1309 1310 return false; 1311 } 1312 #endif // PRODUCT 1313 1314 // Move class loader data from main list to the unloaded list for unloading 1315 // and deallocation later. 1316 bool ClassLoaderDataGraph::do_unloading(bool do_cleaning) { 1317 1318 // Indicate whether safepoint cleanup is needed. 1319 _safepoint_cleanup_needed |= do_cleaning; 1320 1321 ClassLoaderData* data = _head; 1322 ClassLoaderData* prev = NULL; 1323 bool seen_dead_loader = false; 1324 uint loaders_processed = 0; 1325 uint loaders_removed = 0; 1326 1327 // Save previous _unloading pointer for CMS which may add to unloading list before 1328 // purging and we don't want to rewalk the previously unloaded class loader data. 1329 _saved_unloading = _unloading; 1330 1331 data = _head; 1332 while (data != NULL) { 1333 if (data->is_alive()) { 1335 data = data->next(); 1336 loaders_processed++; 1337 continue; 1338 } 1339 seen_dead_loader = true; 1340 loaders_removed++; 1341 ClassLoaderData* dead = data; 1342 dead->unload(); 1343 data = data->next(); 1344 // Remove from loader list. 1345 // This class loader data will no longer be found 1346 // in the ClassLoaderDataGraph. 1347 if (prev != NULL) { 1348 prev->set_next(data); 1349 } else { 1350 assert(dead == _head, "sanity check"); 1351 _head = data; 1352 } 1353 dead->set_next(_unloading); 1354 _unloading = dead; 1355 } 1356 1357 log_debug(class, loader, data)("do_unloading: loaders processed %u, loaders removed %u", loaders_processed, loaders_removed); 1358 1359 return seen_dead_loader; 1360 } 1361 1362 // There's at least one dead class loader. Purge refererences of healthy module 1363 // reads lists and package export lists to modules belonging to dead loaders. 1364 void ClassLoaderDataGraph::clean_module_and_package_info() { 1365 ClassLoaderData* data = _head; 1366 while (data != NULL) { 1367 // Remove entries in the dictionary of live class loader that have 1368 // initiated loading classes in a dead class loader. 1369 if (data->dictionary() != NULL) { 1370 data->dictionary()->do_unloading(); 1371 } 1372 // Walk a ModuleEntry's reads, and a PackageEntry's exports 1373 // lists to determine if there are modules on those lists that are now 1374 // dead and should be removed. A module's life cycle is equivalent |