src/hotspot/share/classfile/classLoaderData.cpp
changeset 51338 aa3bfacc912c
parent 51334 cc2c79d22508
child 51375 b812a85b3aa4
equal deleted inserted replaced
51337:0bcb90968b3c 51338:aa3bfacc912c
    93 
    93 
    94   _the_null_class_loader_data = new ClassLoaderData(Handle(), false);
    94   _the_null_class_loader_data = new ClassLoaderData(Handle(), false);
    95   ClassLoaderDataGraph::_head = _the_null_class_loader_data;
    95   ClassLoaderDataGraph::_head = _the_null_class_loader_data;
    96   assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
    96   assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
    97 
    97 
    98   LogTarget(Debug, class, loader, data) lt;
    98   LogTarget(Trace, class, loader, data) lt;
    99   if (lt.is_enabled()) {
    99   if (lt.is_enabled()) {
   100     ResourceMark rm;
   100     ResourceMark rm;
   101     LogStream ls(lt);
   101     LogStream ls(lt);
   102     ls.print("create ");
   102     ls.print("create ");
   103     _the_null_class_loader_data->print_value_on(&ls);
   103     _the_null_class_loader_data->print_value_on(&ls);
   593 }
   593 }
   594 
   594 
   595 void ClassLoaderData::unload() {
   595 void ClassLoaderData::unload() {
   596   _unloading = true;
   596   _unloading = true;
   597 
   597 
   598   LogTarget(Debug, class, loader, data) lt;
   598   LogTarget(Trace, class, loader, data) lt;
   599   if (lt.is_enabled()) {
   599   if (lt.is_enabled()) {
   600     ResourceMark rm;
   600     ResourceMark rm;
   601     LogStream ls(lt);
   601     LogStream ls(lt);
   602     ls.print("unload");
   602     ls.print("unload");
   603     print_value_on(&ls);
   603     print_value_on(&ls);
   604     ls.cr();
   604     ls.cr();
   605   }
   605   }
   606 
   606 
   607   // Some items on the _deallocate_list need to free their C heap structures
   607   // Some items on the _deallocate_list need to free their C heap structures
   608   // if they are not already on the _klasses list.
   608   // if they are not already on the _klasses list.
   609   unload_deallocate_list();
   609   free_deallocate_list_C_heap_structures();
   610 
   610 
   611   // Tell serviceability tools these classes are unloading
   611   // Tell serviceability tools these classes are unloading
   612   // after erroneous classes are released.
   612   // after erroneous classes are released.
   613   classes_do(InstanceKlass::notify_unload_class);
   613   classes_do(InstanceKlass::notify_unload_class);
   614 
   614 
   847     dest = _handles.add(h());
   847     dest = _handles.add(h());
   848   }
   848   }
   849 }
   849 }
   850 
   850 
   851 // Add this metadata pointer to be freed when it's safe.  This is only during
   851 // Add this metadata pointer to be freed when it's safe.  This is only during
   852 // class unloading because Handles might point to this metadata field.
   852 // a safepoint which checks if handles point to this metadata field.
   853 void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
   853 void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
   854   // Metadata in shared region isn't deleted.
   854   // Metadata in shared region isn't deleted.
   855   if (!m->is_shared()) {
   855   if (!m->is_shared()) {
   856     MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   856     MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   857     if (_deallocate_list == NULL) {
   857     if (_deallocate_list == NULL) {
   858       _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);
   858       _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);
   859     }
   859     }
   860     _deallocate_list->append_if_missing(m);
   860     _deallocate_list->append_if_missing(m);
       
   861     log_debug(class, loader, data)("deallocate added for %s", m->print_value_string());
       
   862     ClassLoaderDataGraph::set_should_clean_deallocate_lists();
   861   }
   863   }
   862 }
   864 }
   863 
   865 
   864 // Deallocate free metadata on the free list.  How useful the PermGen was!
   866 // Deallocate free metadata on the free list.  How useful the PermGen was!
   865 void ClassLoaderData::free_deallocate_list() {
   867 void ClassLoaderData::free_deallocate_list() {
   889       // Metadata is alive.
   891       // Metadata is alive.
   890       // If scratch_class is on stack then it shouldn't be on this list!
   892       // If scratch_class is on stack then it shouldn't be on this list!
   891       assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(),
   893       assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(),
   892              "scratch classes on this list should be dead");
   894              "scratch classes on this list should be dead");
   893       // Also should assert that other metadata on the list was found in handles.
   895       // Also should assert that other metadata on the list was found in handles.
   894     }
   896       // Some cleaning remains.
   895   }
   897       ClassLoaderDataGraph::set_should_clean_deallocate_lists();
       
   898     }
       
   899   }
       
   900 }
       
   901 
       
   902 void ClassLoaderDataGraph::clean_deallocate_lists(bool walk_previous_versions) {
       
   903   uint loaders_processed = 0;
       
   904   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
       
   905     // is_alive check will be necessary for concurrent class unloading.
       
   906     if (cld->is_alive()) {
       
   907       // clean metaspace
       
   908       if (walk_previous_versions) {
       
   909         cld->classes_do(InstanceKlass::purge_previous_versions);
       
   910       }
       
   911       cld->free_deallocate_list();
       
   912       loaders_processed++;
       
   913     }
       
   914   }
       
   915   log_debug(class, loader, data)("clean_deallocate_lists: loaders processed %u %s",
       
   916                                  loaders_processed, walk_previous_versions ? "walk_previous_versions" : "");
       
   917 }
       
   918 
       
   919 void ClassLoaderDataGraph::walk_metadata_and_clean_metaspaces() {
       
   920   assert(SafepointSynchronize::is_at_safepoint(), "must only be called at safepoint");
       
   921 
       
   922   _should_clean_deallocate_lists = false; // assume everything gets cleaned
       
   923 
       
   924   // Mark metadata seen on the stack so we can delete unreferenced entries.
       
   925   // Walk all metadata, including the expensive code cache walk, only for class redefinition.
       
   926   // The MetadataOnStackMark walk during redefinition saves previous versions if it finds old methods
       
   927   // on the stack or in the code cache, so we only have to repeat the full walk if
       
   928   // they were found at that time.
       
   929   // TODO: have redefinition clean old methods out of the code cache.  They still exist in some places.
       
   930   bool walk_all_metadata = InstanceKlass::has_previous_versions_and_reset();
       
   931 
       
   932   MetadataOnStackMark md_on_stack(walk_all_metadata);
       
   933   clean_deallocate_lists(walk_all_metadata);
   896 }
   934 }
   897 
   935 
   898 // This is distinct from free_deallocate_list.  For class loader data that are
   936 // This is distinct from free_deallocate_list.  For class loader data that are
   899 // unloading, this frees the C heap memory for items on the list, and unlinks
   937 // unloading, this frees the C heap memory for items on the list, and unlinks
   900 // scratch or error classes so that unloading events aren't triggered for these
   938 // scratch or error classes so that unloading events aren't triggered for these
   901 // classes. The metadata is removed with the unloading metaspace.
   939 // classes. The metadata is removed with the unloading metaspace.
   902 // There isn't C heap memory allocated for methods, so nothing is done for them.
   940 // There isn't C heap memory allocated for methods, so nothing is done for them.
   903 void ClassLoaderData::unload_deallocate_list() {
   941 void ClassLoaderData::free_deallocate_list_C_heap_structures() {
   904   // Don't need lock, at safepoint
   942   // Don't need lock, at safepoint
   905   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
   943   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
   906   assert(is_unloading(), "only called for ClassLoaderData that are unloading");
   944   assert(is_unloading(), "only called for ClassLoaderData that are unloading");
   907   if (_deallocate_list == NULL) {
   945   if (_deallocate_list == NULL) {
   908     return;
   946     return;
   909   }
   947   }
   910   // Go backwards because this removes entries that are freed.
   948   // Go backwards because this removes entries that are freed.
   911   for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
   949   for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
   912     Metadata* m = _deallocate_list->at(i);
   950     Metadata* m = _deallocate_list->at(i);
   913     assert (!m->on_stack(), "wouldn't be unloading if this were so");
       
   914     _deallocate_list->remove_at(i);
   951     _deallocate_list->remove_at(i);
   915     if (m->is_constantPool()) {
   952     if (m->is_constantPool()) {
   916       ((ConstantPool*)m)->release_C_heap_structures();
   953       ((ConstantPool*)m)->release_C_heap_structures();
   917     } else if (m->is_klass()) {
   954     } else if (m->is_klass()) {
   918       InstanceKlass* ik = (InstanceKlass*)m;
   955       InstanceKlass* ik = (InstanceKlass*)m;
  1024 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
  1061 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
  1025 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
  1062 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
  1026 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
  1063 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
  1027 
  1064 
  1028 bool ClassLoaderDataGraph::_should_purge = false;
  1065 bool ClassLoaderDataGraph::_should_purge = false;
       
  1066 bool ClassLoaderDataGraph::_should_clean_deallocate_lists = false;
       
  1067 bool ClassLoaderDataGraph::_safepoint_cleanup_needed = false;
  1029 bool ClassLoaderDataGraph::_metaspace_oom = false;
  1068 bool ClassLoaderDataGraph::_metaspace_oom = false;
  1030 
  1069 
  1031 // Add a new class loader data node to the list.  Assign the newly created
  1070 // Add a new class loader data node to the list.  Assign the newly created
  1032 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
  1071 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
  1033 ClassLoaderData* ClassLoaderDataGraph::add_to_graph(Handle loader, bool is_anonymous) {
  1072 ClassLoaderData* ClassLoaderDataGraph::add_to_graph(Handle loader, bool is_anonymous) {
  1054 
  1093 
  1055   do {
  1094   do {
  1056     cld->set_next(next);
  1095     cld->set_next(next);
  1057     ClassLoaderData* exchanged = Atomic::cmpxchg(cld, list_head, next);
  1096     ClassLoaderData* exchanged = Atomic::cmpxchg(cld, list_head, next);
  1058     if (exchanged == next) {
  1097     if (exchanged == next) {
  1059       LogTarget(Debug, class, loader, data) lt;
  1098       LogTarget(Trace, class, loader, data) lt;
  1060       if (lt.is_enabled()) {
  1099       if (lt.is_enabled()) {
  1061         ResourceMark rm;
  1100         ResourceMark rm;
  1062         LogStream ls(lt);
  1101         LogStream ls(lt);
  1063         ls.print("create ");
  1102         ls.print("create ");
  1064         cld->print_value_on(&ls);
  1103         cld->print_value_on(&ls);
  1335 }
  1374 }
  1336 #endif // INCLUDE_JFR
  1375 #endif // INCLUDE_JFR
  1337 
  1376 
  1338 // Move class loader data from main list to the unloaded list for unloading
  1377 // Move class loader data from main list to the unloaded list for unloading
  1339 // and deallocation later.
  1378 // and deallocation later.
  1340 bool ClassLoaderDataGraph::do_unloading(bool clean_previous_versions) {
  1379 bool ClassLoaderDataGraph::do_unloading(bool do_cleaning) {
       
  1380 
       
  1381   // Indicate whether safepoint cleanup is needed.
       
  1382   _safepoint_cleanup_needed |= do_cleaning;
  1341 
  1383 
  1342   ClassLoaderData* data = _head;
  1384   ClassLoaderData* data = _head;
  1343   ClassLoaderData* prev = NULL;
  1385   ClassLoaderData* prev = NULL;
  1344   bool seen_dead_loader = false;
  1386   bool seen_dead_loader = false;
  1345   uint loaders_processed = 0;
  1387   uint loaders_processed = 0;
  1346   uint loaders_removed = 0;
  1388   uint loaders_removed = 0;
  1347 
  1389 
  1348   // Mark metadata seen on the stack only so we can delete unneeded entries.
       
  1349   // Only walk all metadata, including the expensive code cache walk, for Full GC
       
  1350   // and only if class redefinition and if there's previous versions of
       
  1351   // Klasses to delete.
       
  1352   bool walk_all_metadata = clean_previous_versions &&
       
  1353                            JvmtiExport::has_redefined_a_class() &&
       
  1354                            InstanceKlass::has_previous_versions_and_reset();
       
  1355   MetadataOnStackMark md_on_stack(walk_all_metadata);
       
  1356 
       
  1357   // Save previous _unloading pointer for CMS which may add to unloading list before
  1390   // Save previous _unloading pointer for CMS which may add to unloading list before
  1358   // purging and we don't want to rewalk the previously unloaded class loader data.
  1391   // purging and we don't want to rewalk the previously unloaded class loader data.
  1359   _saved_unloading = _unloading;
  1392   _saved_unloading = _unloading;
  1360 
  1393 
  1361   data = _head;
  1394   data = _head;
  1362   while (data != NULL) {
  1395   while (data != NULL) {
  1363     if (data->is_alive()) {
  1396     if (data->is_alive()) {
  1364       // clean metaspace
       
  1365       if (walk_all_metadata) {
       
  1366         data->classes_do(InstanceKlass::purge_previous_versions);
       
  1367       }
       
  1368       data->free_deallocate_list();
       
  1369       prev = data;
  1397       prev = data;
  1370       data = data->next();
  1398       data = data->next();
  1371       loaders_processed++;
  1399       loaders_processed++;
  1372       continue;
  1400       continue;
  1373     }
  1401     }