< prev index next >

src/hotspot/share/classfile/classLoaderData.cpp

Print this page




  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(Debug, 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);
 104     ls.cr();
 105   }
 106 }
 107 
 108 // Obtain and set the class loader's name within the ClassLoaderData so
 109 // it will be available for error messages, logging, JFR, etc.  The name
 110 // and klass are available after the class_loader oop is no longer alive,
 111 // during unloading.
 112 void ClassLoaderData::initialize_name(Handle class_loader) {
 113   Thread* THREAD = Thread::current();
 114   ResourceMark rm(THREAD);
 115 
 116   // Obtain the class loader's name.  If the class loader's name was not
 117   // explicitly set during construction, the CLD's _name field will be null.
 118   oop cl_name = java_lang_ClassLoader::name(class_loader());


 575         prev->set_next_link(next);
 576       }
 577 
 578       if (k->is_array_klass()) {
 579         ClassLoaderDataGraph::dec_array_classes(1);
 580       } else {
 581         ClassLoaderDataGraph::dec_instance_classes(1);
 582       }
 583 
 584       return;
 585     }
 586     prev = k;
 587     assert(k != k->next_link(), "no loops!");
 588   }
 589   ShouldNotReachHere();   // should have found this class!!
 590 }
 591 
 592 void ClassLoaderData::unload() {
 593   _unloading = true;
 594 
 595   LogTarget(Debug, class, loader, data) lt;
 596   if (lt.is_enabled()) {
 597     ResourceMark rm;
 598     LogStream ls(lt);
 599     ls.print("unload");
 600     print_value_on(&ls);
 601     ls.cr();
 602   }
 603 
 604   // Some items on the _deallocate_list need to free their C heap structures
 605   // if they are not already on the _klasses list.
 606   unload_deallocate_list();
 607 
 608   // Tell serviceability tools these classes are unloading
 609   // after erroneous classes are released.
 610   classes_do(InstanceKlass::notify_unload_class);
 611 
 612   // Clean up global class iterator for compiler
 613   static_klass_iterator.adjust_saved_class(this);
 614 }
 615 
 616 ModuleEntryTable* ClassLoaderData::modules() {
 617   // Lazily create the module entry table at first request.
 618   // Lock-free access requires load_acquire.
 619   ModuleEntryTable* modules = OrderAccess::load_acquire(&_modules);
 620   if (modules == NULL) {
 621     MutexLocker m1(Module_lock);
 622     // Check if _modules got allocated while we were waiting for this lock.
 623     if ((modules = _modules) == NULL) {
 624       modules = new ModuleEntryTable(ModuleEntryTable::_moduletable_entry_size);
 625 
 626       {


 829 
 830 void ClassLoaderData::remove_handle(OopHandle h) {
 831   assert(!is_unloading(), "Do not remove a handle for a CLD that is unloading");
 832   oop* ptr = h.ptr_raw();
 833   if (ptr != NULL) {
 834     assert(_handles.owner_of(ptr), "Got unexpected handle " PTR_FORMAT, p2i(ptr));
 835     NativeAccess<>::oop_store(ptr, oop(NULL));
 836   }
 837 }
 838 
 839 void ClassLoaderData::init_handle_locked(OopHandle& dest, Handle h) {
 840   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
 841   if (dest.resolve() != NULL) {
 842     return;
 843   } else {
 844     dest = _handles.add(h());
 845   }
 846 }
 847 
 848 // Add this metadata pointer to be freed when it's safe.  This is only during
 849 // class unloading because Handles might point to this metadata field.
 850 void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
 851   // Metadata in shared region isn't deleted.
 852   if (!m->is_shared()) {
 853     MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
 854     if (_deallocate_list == NULL) {
 855       _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);
 856     }
 857     _deallocate_list->append_if_missing(m);


 858   }
 859 }
 860 
 861 // Deallocate free metadata on the free list.  How useful the PermGen was!
 862 void ClassLoaderData::free_deallocate_list() {
 863   // Don't need lock, at safepoint
 864   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
 865   assert(!is_unloading(), "only called for ClassLoaderData that are not unloading");
 866   if (_deallocate_list == NULL) {
 867     return;
 868   }
 869   // Go backwards because this removes entries that are freed.
 870   for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
 871     Metadata* m = _deallocate_list->at(i);
 872     if (!m->on_stack()) {
 873       _deallocate_list->remove_at(i);
 874       // There are only three types of metadata that we deallocate directly.
 875       // Cast them so they can be used by the template function.
 876       if (m->is_method()) {
 877         MetadataFactory::free_metadata(this, (Method*)m);
 878       } else if (m->is_constantPool()) {
 879         MetadataFactory::free_metadata(this, (ConstantPool*)m);
 880       } else if (m->is_klass()) {
 881         MetadataFactory::free_metadata(this, (InstanceKlass*)m);
 882       } else {
 883         ShouldNotReachHere();
 884       }
 885     } else {
 886       // Metadata is alive.
 887       // If scratch_class is on stack then it shouldn't be on this list!
 888       assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(),
 889              "scratch classes on this list should be dead");
 890       // Also should assert that other metadata on the list was found in handles.


 891     }
 892   }
 893 }
 894 


































 895 // This is distinct from free_deallocate_list.  For class loader data that are
 896 // unloading, this frees the C heap memory for items on the list, and unlinks
 897 // scratch or error classes so that unloading events aren't triggered for these
 898 // classes. The metadata is removed with the unloading metaspace.
 899 // There isn't C heap memory allocated for methods, so nothing is done for them.
 900 void ClassLoaderData::unload_deallocate_list() {
 901   // Don't need lock, at safepoint
 902   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
 903   assert(is_unloading(), "only called for ClassLoaderData that are unloading");
 904   if (_deallocate_list == NULL) {
 905     return;
 906   }
 907   // Go backwards because this removes entries that are freed.
 908   for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
 909     Metadata* m = _deallocate_list->at(i);
 910     assert (!m->on_stack(), "wouldn't be unloading if this were so");
 911     _deallocate_list->remove_at(i);
 912     if (m->is_constantPool()) {
 913       ((ConstantPool*)m)->release_C_heap_structures();
 914     } else if (m->is_klass()) {
 915       InstanceKlass* ik = (InstanceKlass*)m;
 916       // also releases ik->constants() C heap memory
 917       InstanceKlass::release_C_heap_structures(ik);
 918       // Remove the class so unloading events aren't triggered for
 919       // this class (scratch or error class) in do_unloading().
 920       remove_class(ik);
 921     }
 922   }
 923 }
 924 
 925 // These anonymous class loaders are to contain classes used for JSR292
 926 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(Handle loader) {
 927   // Add a new class loader data to the graph.
 928   return ClassLoaderDataGraph::add(loader, true);
 929 }
 930 


1006     assert(k != k->next_link(), "no loops!");
1007   }
1008 }
1009 
1010 bool ClassLoaderData::contains_klass(Klass* klass) {
1011   // Lock-free access requires load_acquire
1012   for (Klass* k = OrderAccess::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
1013     if (k == klass) return true;
1014   }
1015   return false;
1016 }
1017 
1018 
1019 // GC root of class loader data created.
1020 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
1021 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
1022 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
1023 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
1024 
1025 bool ClassLoaderDataGraph::_should_purge = false;


1026 bool ClassLoaderDataGraph::_metaspace_oom = false;
1027 
1028 // Add a new class loader data node to the list.  Assign the newly created
1029 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
1030 ClassLoaderData* ClassLoaderDataGraph::add_to_graph(Handle loader, bool is_anonymous) {
1031   NoSafepointVerifier no_safepoints; // we mustn't GC until we've installed the
1032                                      // ClassLoaderData in the graph since the CLD
1033                                      // contains oops in _handles that must be walked.
1034 
1035   ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous);
1036 
1037   if (!is_anonymous) {
1038     // First, Atomically set it
1039     ClassLoaderData* old = java_lang_ClassLoader::cmpxchg_loader_data(cld, loader(), NULL);
1040     if (old != NULL) {
1041       delete cld;
1042       // Returns the data.
1043       return old;
1044     }
1045   }
1046 
1047   // We won the race, and therefore the task of adding the data to the list of
1048   // class loader data
1049   ClassLoaderData** list_head = &_head;
1050   ClassLoaderData* next = _head;
1051 
1052   do {
1053     cld->set_next(next);
1054     ClassLoaderData* exchanged = Atomic::cmpxchg(cld, list_head, next);
1055     if (exchanged == next) {
1056       LogTarget(Debug, class, loader, data) lt;
1057       if (lt.is_enabled()) {
1058         ResourceMark rm;
1059         LogStream ls(lt);
1060         ls.print("create ");
1061         cld->print_value_on(&ls);
1062         ls.cr();
1063       }
1064       return cld;
1065     }
1066     next = exchanged;
1067   } while (true);
1068 }
1069 
1070 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous) {
1071   ClassLoaderData* loader_data = add_to_graph(loader, is_anonymous);
1072   // Initialize _name and _name_and_id after the loader data is added to the
1073   // CLDG because adding the Symbol for _name and _name_and_id might safepoint.
1074   if (loader.not_null()) {
1075     loader_data->initialize_name(loader);
1076   }


1317   event.set_endtime(class_unload_time);
1318   event.set_unloadedClass(k);
1319   event.set_definingClassLoader(k->class_loader_data());
1320   event.commit();
1321 }
1322 
1323 static void post_class_unload_events() {
1324   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
1325   if (Jfr::is_enabled()) {
1326     if (EventClassUnload::is_enabled()) {
1327       class_unload_time = Ticks::now();
1328       ClassLoaderDataGraph::classes_unloading_do(&post_class_unload_event);
1329     }
1330     Jfr::on_unloading_classes();
1331   }
1332 }
1333 #endif // INCLUDE_JFR
1334 
1335 // Move class loader data from main list to the unloaded list for unloading
1336 // and deallocation later.
1337 bool ClassLoaderDataGraph::do_unloading(bool clean_previous_versions) {



1338 
1339   ClassLoaderData* data = _head;
1340   ClassLoaderData* prev = NULL;
1341   bool seen_dead_loader = false;
1342   uint loaders_processed = 0;
1343   uint loaders_removed = 0;
1344 
1345   // Mark metadata seen on the stack only so we can delete unneeded entries.
1346   // Only walk all metadata, including the expensive code cache walk, for Full GC
1347   // and only if class redefinition and if there's previous versions of
1348   // Klasses to delete.
1349   bool walk_all_metadata = clean_previous_versions &&
1350                            JvmtiExport::has_redefined_a_class() &&
1351                            InstanceKlass::has_previous_versions_and_reset();
1352   MetadataOnStackMark md_on_stack(walk_all_metadata);
1353 
1354   // Save previous _unloading pointer for CMS which may add to unloading list before
1355   // purging and we don't want to rewalk the previously unloaded class loader data.
1356   _saved_unloading = _unloading;
1357 
1358   data = _head;
1359   while (data != NULL) {
1360     if (data->is_alive()) {
1361       // clean metaspace
1362       if (walk_all_metadata) {
1363         data->classes_do(InstanceKlass::purge_previous_versions);
1364       }
1365       data->free_deallocate_list();
1366       prev = data;
1367       data = data->next();
1368       loaders_processed++;
1369       continue;
1370     }
1371     seen_dead_loader = true;
1372     loaders_removed++;
1373     ClassLoaderData* dead = data;
1374     dead->unload();
1375     data = data->next();
1376     // Remove from loader list.
1377     // This class loader data will no longer be found
1378     // in the ClassLoaderDataGraph.
1379     if (prev != NULL) {
1380       prev->set_next(data);
1381     } else {
1382       assert(dead == _head, "sanity check");
1383       _head = data;
1384     }
1385     dead->set_next(_unloading);




  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);
 104     ls.cr();
 105   }
 106 }
 107 
 108 // Obtain and set the class loader's name within the ClassLoaderData so
 109 // it will be available for error messages, logging, JFR, etc.  The name
 110 // and klass are available after the class_loader oop is no longer alive,
 111 // during unloading.
 112 void ClassLoaderData::initialize_name(Handle class_loader) {
 113   Thread* THREAD = Thread::current();
 114   ResourceMark rm(THREAD);
 115 
 116   // Obtain the class loader's name.  If the class loader's name was not
 117   // explicitly set during construction, the CLD's _name field will be null.
 118   oop cl_name = java_lang_ClassLoader::name(class_loader());


 575         prev->set_next_link(next);
 576       }
 577 
 578       if (k->is_array_klass()) {
 579         ClassLoaderDataGraph::dec_array_classes(1);
 580       } else {
 581         ClassLoaderDataGraph::dec_instance_classes(1);
 582       }
 583 
 584       return;
 585     }
 586     prev = k;
 587     assert(k != k->next_link(), "no loops!");
 588   }
 589   ShouldNotReachHere();   // should have found this class!!
 590 }
 591 
 592 void ClassLoaderData::unload() {
 593   _unloading = true;
 594 
 595   LogTarget(Trace, class, loader, data) lt;
 596   if (lt.is_enabled()) {
 597     ResourceMark rm;
 598     LogStream ls(lt);
 599     ls.print("unload");
 600     print_value_on(&ls);
 601     ls.cr();
 602   }
 603 
 604   // Some items on the _deallocate_list need to free their C heap structures
 605   // if they are not already on the _klasses list.
 606   free_deallocate_list_C_heap_structures();
 607 
 608   // Tell serviceability tools these classes are unloading
 609   // after erroneous classes are released.
 610   classes_do(InstanceKlass::notify_unload_class);
 611 
 612   // Clean up global class iterator for compiler
 613   static_klass_iterator.adjust_saved_class(this);
 614 }
 615 
 616 ModuleEntryTable* ClassLoaderData::modules() {
 617   // Lazily create the module entry table at first request.
 618   // Lock-free access requires load_acquire.
 619   ModuleEntryTable* modules = OrderAccess::load_acquire(&_modules);
 620   if (modules == NULL) {
 621     MutexLocker m1(Module_lock);
 622     // Check if _modules got allocated while we were waiting for this lock.
 623     if ((modules = _modules) == NULL) {
 624       modules = new ModuleEntryTable(ModuleEntryTable::_moduletable_entry_size);
 625 
 626       {


 829 
 830 void ClassLoaderData::remove_handle(OopHandle h) {
 831   assert(!is_unloading(), "Do not remove a handle for a CLD that is unloading");
 832   oop* ptr = h.ptr_raw();
 833   if (ptr != NULL) {
 834     assert(_handles.owner_of(ptr), "Got unexpected handle " PTR_FORMAT, p2i(ptr));
 835     NativeAccess<>::oop_store(ptr, oop(NULL));
 836   }
 837 }
 838 
 839 void ClassLoaderData::init_handle_locked(OopHandle& dest, Handle h) {
 840   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
 841   if (dest.resolve() != NULL) {
 842     return;
 843   } else {
 844     dest = _handles.add(h());
 845   }
 846 }
 847 
 848 // Add this metadata pointer to be freed when it's safe.  This is only during
 849 // a safepoint which checks if handles point to this metadata field.
 850 void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
 851   // Metadata in shared region isn't deleted.
 852   if (!m->is_shared()) {
 853     MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
 854     if (_deallocate_list == NULL) {
 855       _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);
 856     }
 857     _deallocate_list->append_if_missing(m);
 858     log_debug(class, loader, data)("deallocate added for %s", m->print_value_string());
 859     ClassLoaderDataGraph::set_should_clean_deallocate_lists();
 860   }
 861 }
 862 
 863 // Deallocate free metadata on the free list.  How useful the PermGen was!
 864 void ClassLoaderData::free_deallocate_list() {
 865   // Don't need lock, at safepoint
 866   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
 867   assert(!is_unloading(), "only called for ClassLoaderData that are not unloading");
 868   if (_deallocate_list == NULL) {
 869     return;
 870   }
 871   // Go backwards because this removes entries that are freed.
 872   for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
 873     Metadata* m = _deallocate_list->at(i);
 874     if (!m->on_stack()) {
 875       _deallocate_list->remove_at(i);
 876       // There are only three types of metadata that we deallocate directly.
 877       // Cast them so they can be used by the template function.
 878       if (m->is_method()) {
 879         MetadataFactory::free_metadata(this, (Method*)m);
 880       } else if (m->is_constantPool()) {
 881         MetadataFactory::free_metadata(this, (ConstantPool*)m);
 882       } else if (m->is_klass()) {
 883         MetadataFactory::free_metadata(this, (InstanceKlass*)m);
 884       } else {
 885         ShouldNotReachHere();
 886       }
 887     } else {
 888       // Metadata is alive.
 889       // If scratch_class is on stack then it shouldn't be on this list!
 890       assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(),
 891              "scratch classes on this list should be dead");
 892       // Also should assert that other metadata on the list was found in handles.
 893       // Some cleaning remains.
 894       ClassLoaderDataGraph::set_should_clean_deallocate_lists();
 895     }
 896   }
 897 }
 898 
 899 void ClassLoaderDataGraph::clean_deallocate_lists(bool walk_previous_versions) {
 900   uint loaders_processed = 0;
 901   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
 902     // is_alive check will be necessary for concurrent class unloading.
 903     if (cld->is_alive()) {
 904       // clean metaspace
 905       if (walk_previous_versions) {
 906         cld->classes_do(InstanceKlass::purge_previous_versions);
 907       }
 908       cld->free_deallocate_list();
 909       loaders_processed++;
 910     }
 911   }
 912   log_debug(class, loader, data)("clean_deallocate_lists: loaders processed %u %s",
 913                                  loaders_processed, walk_previous_versions ? "walk_previous_versions" : "");
 914 }
 915 
 916 void ClassLoaderDataGraph::walk_metadata_and_clean_metaspaces() {
 917   assert(SafepointSynchronize::is_at_safepoint(), "must only be called at safepoint");
 918 
 919   _should_clean_deallocate_lists = false; // assume everything gets cleaned
 920 
 921   // Mark metadata seen on the stack so we can delete unreferenced entries.
 922   // Walk all metadata, including the expensive code cache walk, only for class redefinition.
 923   // The MetadataOnStackMark walk during redefinition saves previous versions if it finds old methods
 924   // on the stack or in the code cache, so we only have to repeat the full walk if
 925   // they were found at that time.
 926   // TODO: have redefinition clean old methods out of the code cache.  They still exist in some places.
 927   bool walk_all_metadata = InstanceKlass::has_previous_versions_and_reset();
 928 
 929   MetadataOnStackMark md_on_stack(walk_all_metadata);
 930   clean_deallocate_lists(walk_all_metadata);
 931 }
 932 
 933 // This is distinct from free_deallocate_list.  For class loader data that are
 934 // unloading, this frees the C heap memory for items on the list, and unlinks
 935 // scratch or error classes so that unloading events aren't triggered for these
 936 // classes. The metadata is removed with the unloading metaspace.
 937 // There isn't C heap memory allocated for methods, so nothing is done for them.
 938 void ClassLoaderData::free_deallocate_list_C_heap_structures() {
 939   // Don't need lock, at safepoint
 940   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
 941   assert(is_unloading(), "only called for ClassLoaderData that are unloading");
 942   if (_deallocate_list == NULL) {
 943     return;
 944   }
 945   // Go backwards because this removes entries that are freed.
 946   for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
 947     Metadata* m = _deallocate_list->at(i);

 948     _deallocate_list->remove_at(i);
 949     if (m->is_constantPool()) {
 950       ((ConstantPool*)m)->release_C_heap_structures();
 951     } else if (m->is_klass()) {
 952       InstanceKlass* ik = (InstanceKlass*)m;
 953       // also releases ik->constants() C heap memory
 954       InstanceKlass::release_C_heap_structures(ik);
 955       // Remove the class so unloading events aren't triggered for
 956       // this class (scratch or error class) in do_unloading().
 957       remove_class(ik);
 958     }
 959   }
 960 }
 961 
 962 // These anonymous class loaders are to contain classes used for JSR292
 963 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(Handle loader) {
 964   // Add a new class loader data to the graph.
 965   return ClassLoaderDataGraph::add(loader, true);
 966 }
 967 


1043     assert(k != k->next_link(), "no loops!");
1044   }
1045 }
1046 
1047 bool ClassLoaderData::contains_klass(Klass* klass) {
1048   // Lock-free access requires load_acquire
1049   for (Klass* k = OrderAccess::load_acquire(&_klasses); k != NULL; k = k->next_link()) {
1050     if (k == klass) return true;
1051   }
1052   return false;
1053 }
1054 
1055 
1056 // GC root of class loader data created.
1057 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
1058 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
1059 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
1060 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
1061 
1062 bool ClassLoaderDataGraph::_should_purge = false;
1063 bool ClassLoaderDataGraph::_should_clean_deallocate_lists = false;
1064 bool ClassLoaderDataGraph::_safepoint_cleanup_needed = false;
1065 bool ClassLoaderDataGraph::_metaspace_oom = false;
1066 
1067 // Add a new class loader data node to the list.  Assign the newly created
1068 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
1069 ClassLoaderData* ClassLoaderDataGraph::add_to_graph(Handle loader, bool is_anonymous) {
1070   NoSafepointVerifier no_safepoints; // we mustn't GC until we've installed the
1071                                      // ClassLoaderData in the graph since the CLD
1072                                      // contains oops in _handles that must be walked.
1073 
1074   ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous);
1075 
1076   if (!is_anonymous) {
1077     // First, Atomically set it
1078     ClassLoaderData* old = java_lang_ClassLoader::cmpxchg_loader_data(cld, loader(), NULL);
1079     if (old != NULL) {
1080       delete cld;
1081       // Returns the data.
1082       return old;
1083     }
1084   }
1085 
1086   // We won the race, and therefore the task of adding the data to the list of
1087   // class loader data
1088   ClassLoaderData** list_head = &_head;
1089   ClassLoaderData* next = _head;
1090 
1091   do {
1092     cld->set_next(next);
1093     ClassLoaderData* exchanged = Atomic::cmpxchg(cld, list_head, next);
1094     if (exchanged == next) {
1095       LogTarget(Trace, class, loader, data) lt;
1096       if (lt.is_enabled()) {
1097         ResourceMark rm;
1098         LogStream ls(lt);
1099         ls.print("create ");
1100         cld->print_value_on(&ls);
1101         ls.cr();
1102       }
1103       return cld;
1104     }
1105     next = exchanged;
1106   } while (true);
1107 }
1108 
1109 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous) {
1110   ClassLoaderData* loader_data = add_to_graph(loader, is_anonymous);
1111   // Initialize _name and _name_and_id after the loader data is added to the
1112   // CLDG because adding the Symbol for _name and _name_and_id might safepoint.
1113   if (loader.not_null()) {
1114     loader_data->initialize_name(loader);
1115   }


1356   event.set_endtime(class_unload_time);
1357   event.set_unloadedClass(k);
1358   event.set_definingClassLoader(k->class_loader_data());
1359   event.commit();
1360 }
1361 
1362 static void post_class_unload_events() {
1363   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
1364   if (Jfr::is_enabled()) {
1365     if (EventClassUnload::is_enabled()) {
1366       class_unload_time = Ticks::now();
1367       ClassLoaderDataGraph::classes_unloading_do(&post_class_unload_event);
1368     }
1369     Jfr::on_unloading_classes();
1370   }
1371 }
1372 #endif // INCLUDE_JFR
1373 
1374 // Move class loader data from main list to the unloaded list for unloading
1375 // and deallocation later.
1376 bool ClassLoaderDataGraph::do_unloading(bool do_cleaning) {
1377 
1378   // Indicate whether safepoint cleanup is needed.
1379   _safepoint_cleanup_needed |= do_cleaning;
1380 
1381   ClassLoaderData* data = _head;
1382   ClassLoaderData* prev = NULL;
1383   bool seen_dead_loader = false;
1384   uint loaders_processed = 0;
1385   uint loaders_removed = 0;
1386 









1387   // Save previous _unloading pointer for CMS which may add to unloading list before
1388   // purging and we don't want to rewalk the previously unloaded class loader data.
1389   _saved_unloading = _unloading;
1390 
1391   data = _head;
1392   while (data != NULL) {
1393     if (data->is_alive()) {





1394       prev = data;
1395       data = data->next();
1396       loaders_processed++;
1397       continue;
1398     }
1399     seen_dead_loader = true;
1400     loaders_removed++;
1401     ClassLoaderData* dead = data;
1402     dead->unload();
1403     data = data->next();
1404     // Remove from loader list.
1405     // This class loader data will no longer be found
1406     // in the ClassLoaderDataGraph.
1407     if (prev != NULL) {
1408       prev->set_next(data);
1409     } else {
1410       assert(dead == _head, "sanity check");
1411       _head = data;
1412     }
1413     dead->set_next(_unloading);


< prev index next >