< prev index next >

src/hotspot/share/classfile/classLoaderDataGraph.cpp

Print this page




  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoaderDataGraph.inline.hpp"
  27 #include "classfile/dictionary.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "classfile/metadataOnStackMark.hpp"
  30 #include "classfile/moduleEntry.hpp"
  31 #include "classfile/packageEntry.hpp"
  32 #include "code/dependencyContext.hpp"
  33 #include "logging/log.hpp"
  34 #include "logging/logStream.hpp"
  35 #include "memory/allocation.inline.hpp"
  36 #include "memory/metaspace.hpp"
  37 #include "memory/resourceArea.hpp"
  38 #include "runtime/atomic.hpp"
  39 #include "runtime/handles.inline.hpp"
  40 #include "runtime/mutex.hpp"
  41 #include "runtime/orderAccess.hpp"
  42 #include "runtime/safepoint.hpp"
  43 #include "runtime/safepointVerifiers.hpp"
  44 #include "utilities/growableArray.hpp"
  45 #include "utilities/macros.hpp"
  46 #include "utilities/ostream.hpp"
  47 
  48 volatile size_t ClassLoaderDataGraph::_num_array_classes = 0;
  49 volatile size_t ClassLoaderDataGraph::_num_instance_classes = 0;
  50 
  51 void ClassLoaderDataGraph::clear_claimed_marks() {
  52   // The claimed marks of the CLDs in the ClassLoaderDataGraph are cleared
  53   // outside a safepoint and without locking the ClassLoaderDataGraph_lock.
  54   // This is required to avoid a deadlock between concurrent GC threads and safepointing.
  55   //
  56   // We need to make sure that the CLD contents are fully visible to the
  57   // reader thread. This is accomplished by acquire/release of the _head,
  58   // and is sufficient.
  59   //
  60   // Any ClassLoaderData added after or during walking the list are prepended to
  61   // _head. Their claim mark need not be handled here.
  62   for (ClassLoaderData* cld = OrderAccess::load_acquire(&_head); cld != NULL; cld = cld->next()) {
  63     cld->clear_claim();
  64   }
  65 }
  66 
  67 // Class iterator used by the compiler.  It gets some number of classes at
  68 // a safepoint to decay invocation counters on the methods.
  69 class ClassLoaderDataGraphKlassIteratorStatic {
  70   ClassLoaderData* _current_loader_data;
  71   Klass*           _current_class_entry;
  72  public:
  73 
  74   ClassLoaderDataGraphKlassIteratorStatic() : _current_loader_data(NULL), _current_class_entry(NULL) {}
  75 
  76   InstanceKlass* try_get_next_class() {
  77     assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
  78     size_t max_classes = ClassLoaderDataGraph::num_instance_classes();
  79     assert(max_classes > 0, "should not be called with no instance classes");
  80     for (size_t i = 0; i < max_classes; ) {
  81 
  82       if (_current_class_entry != NULL) {


 162 }
 163 
 164 void ClassLoaderDataGraph::walk_metadata_and_clean_metaspaces() {
 165   assert(SafepointSynchronize::is_at_safepoint(), "must only be called at safepoint");
 166 
 167   _should_clean_deallocate_lists = false; // assume everything gets cleaned
 168 
 169   // Mark metadata seen on the stack so we can delete unreferenced entries.
 170   // Walk all metadata, including the expensive code cache walk, only for class redefinition.
 171   // The MetadataOnStackMark walk during redefinition saves previous versions if it finds old methods
 172   // on the stack or in the code cache, so we only have to repeat the full walk if
 173   // they were found at that time.
 174   // TODO: have redefinition clean old methods out of the code cache.  They still exist in some places.
 175   bool walk_all_metadata = InstanceKlass::has_previous_versions_and_reset();
 176 
 177   MetadataOnStackMark md_on_stack(walk_all_metadata, /*redefinition_walk*/false);
 178   clean_deallocate_lists(walk_all_metadata);
 179 }
 180 
 181 // GC root of class loader data created.
 182 ClassLoaderData* volatile ClassLoaderDataGraph::_head = NULL;
 183 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
 184 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
 185 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
 186 
 187 bool ClassLoaderDataGraph::_should_purge = false;
 188 bool ClassLoaderDataGraph::_should_clean_deallocate_lists = false;
 189 bool ClassLoaderDataGraph::_safepoint_cleanup_needed = false;
 190 bool ClassLoaderDataGraph::_metaspace_oom = false;
 191 
 192 // Add a new class loader data node to the list.  Assign the newly created
 193 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
 194 ClassLoaderData* ClassLoaderDataGraph::add_to_graph(Handle loader, bool is_unsafe_anonymous) {
 195 
 196   assert_lock_strong(ClassLoaderDataGraph_lock);
 197 
 198   ClassLoaderData* cld;
 199 
 200   // First check if another thread beat us to creating the CLD and installing
 201   // it into the loader while we were waiting for the lock.
 202   if (!is_unsafe_anonymous && loader.not_null()) {
 203     cld = java_lang_ClassLoader::loader_data_acquire(loader());
 204     if (cld != NULL) {
 205       return cld;
 206     }
 207   }
 208 
 209   // We mustn't GC until we've installed the ClassLoaderData in the Graph since the CLD
 210   // contains oops in _handles that must be walked.  GC doesn't walk CLD from the
 211   // loader oop in all collections, particularly young collections.
 212   NoSafepointVerifier no_safepoints;
 213 
 214   cld = new ClassLoaderData(loader, is_unsafe_anonymous);
 215 
 216   // First install the new CLD to the Graph.
 217   cld->set_next(_head);
 218   OrderAccess::release_store(&_head, cld);
 219 
 220   // Next associate with the class_loader.
 221   if (!is_unsafe_anonymous) {
 222     // Use OrderAccess, since readers need to get the loader_data only after
 223     // it's added to the Graph
 224     java_lang_ClassLoader::release_set_loader_data(loader(), cld);
 225   }
 226 
 227   // Lastly log, if requested
 228   LogTarget(Trace, class, loader, data) lt;
 229   if (lt.is_enabled()) {
 230     ResourceMark rm;
 231     LogStream ls(lt);
 232     ls.print("create ");
 233     cld->print_value_on(&ls);
 234     ls.cr();
 235   }
 236   return cld;
 237 }
 238 




  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/classLoaderDataGraph.inline.hpp"
  27 #include "classfile/dictionary.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "classfile/metadataOnStackMark.hpp"
  30 #include "classfile/moduleEntry.hpp"
  31 #include "classfile/packageEntry.hpp"
  32 #include "code/dependencyContext.hpp"
  33 #include "logging/log.hpp"
  34 #include "logging/logStream.hpp"
  35 #include "memory/allocation.inline.hpp"
  36 #include "memory/metaspace.hpp"
  37 #include "memory/resourceArea.hpp"
  38 #include "runtime/atomic.hpp"
  39 #include "runtime/handles.inline.hpp"
  40 #include "runtime/mutex.hpp"

  41 #include "runtime/safepoint.hpp"
  42 #include "runtime/safepointVerifiers.hpp"
  43 #include "utilities/growableArray.hpp"
  44 #include "utilities/macros.hpp"
  45 #include "utilities/ostream.hpp"
  46 
  47 volatile size_t ClassLoaderDataGraph::_num_array_classes = 0;
  48 volatile size_t ClassLoaderDataGraph::_num_instance_classes = 0;
  49 
  50 void ClassLoaderDataGraph::clear_claimed_marks() {
  51   assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock);
  52   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {









  53     cld->clear_claim();
  54   }
  55 }
  56 
  57 // Class iterator used by the compiler.  It gets some number of classes at
  58 // a safepoint to decay invocation counters on the methods.
  59 class ClassLoaderDataGraphKlassIteratorStatic {
  60   ClassLoaderData* _current_loader_data;
  61   Klass*           _current_class_entry;
  62  public:
  63 
  64   ClassLoaderDataGraphKlassIteratorStatic() : _current_loader_data(NULL), _current_class_entry(NULL) {}
  65 
  66   InstanceKlass* try_get_next_class() {
  67     assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
  68     size_t max_classes = ClassLoaderDataGraph::num_instance_classes();
  69     assert(max_classes > 0, "should not be called with no instance classes");
  70     for (size_t i = 0; i < max_classes; ) {
  71 
  72       if (_current_class_entry != NULL) {


 152 }
 153 
 154 void ClassLoaderDataGraph::walk_metadata_and_clean_metaspaces() {
 155   assert(SafepointSynchronize::is_at_safepoint(), "must only be called at safepoint");
 156 
 157   _should_clean_deallocate_lists = false; // assume everything gets cleaned
 158 
 159   // Mark metadata seen on the stack so we can delete unreferenced entries.
 160   // Walk all metadata, including the expensive code cache walk, only for class redefinition.
 161   // The MetadataOnStackMark walk during redefinition saves previous versions if it finds old methods
 162   // on the stack or in the code cache, so we only have to repeat the full walk if
 163   // they were found at that time.
 164   // TODO: have redefinition clean old methods out of the code cache.  They still exist in some places.
 165   bool walk_all_metadata = InstanceKlass::has_previous_versions_and_reset();
 166 
 167   MetadataOnStackMark md_on_stack(walk_all_metadata, /*redefinition_walk*/false);
 168   clean_deallocate_lists(walk_all_metadata);
 169 }
 170 
 171 // GC root of class loader data created.
 172 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
 173 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
 174 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
 175 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
 176 
 177 bool ClassLoaderDataGraph::_should_purge = false;
 178 bool ClassLoaderDataGraph::_should_clean_deallocate_lists = false;
 179 bool ClassLoaderDataGraph::_safepoint_cleanup_needed = false;
 180 bool ClassLoaderDataGraph::_metaspace_oom = false;
 181 
 182 // Add a new class loader data node to the list.  Assign the newly created
 183 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
 184 ClassLoaderData* ClassLoaderDataGraph::add_to_graph(Handle loader, bool is_unsafe_anonymous) {
 185 
 186   assert_lock_strong(ClassLoaderDataGraph_lock);
 187 
 188   ClassLoaderData* cld;
 189 
 190   // First check if another thread beat us to creating the CLD and installing
 191   // it into the loader while we were waiting for the lock.
 192   if (!is_unsafe_anonymous && loader.not_null()) {
 193     cld = java_lang_ClassLoader::loader_data_acquire(loader());
 194     if (cld != NULL) {
 195       return cld;
 196     }
 197   }
 198 
 199   // We mustn't GC until we've installed the ClassLoaderData in the Graph since the CLD
 200   // contains oops in _handles that must be walked.  GC doesn't walk CLD from the
 201   // loader oop in all collections, particularly young collections.
 202   NoSafepointVerifier no_safepoints;
 203 
 204   cld = new ClassLoaderData(loader, is_unsafe_anonymous);
 205 
 206   // First install the new CLD to the Graph.
 207   cld->set_next(_head);
 208   _head = cld;
 209 
 210   // Next associate with the class_loader.
 211   if (!is_unsafe_anonymous) {
 212     // Use OrderAccess, since readers need to get the loader_data only after
 213     // it's added to the Graph
 214     java_lang_ClassLoader::release_set_loader_data(loader(), cld);
 215   }
 216 
 217   // Lastly log, if requested
 218   LogTarget(Trace, class, loader, data) lt;
 219   if (lt.is_enabled()) {
 220     ResourceMark rm;
 221     LogStream ls(lt);
 222     ls.print("create ");
 223     cld->print_value_on(&ls);
 224     ls.cr();
 225   }
 226   return cld;
 227 }
 228 


< prev index next >