1 /*
   2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  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) {
  73         Klass* k = _current_class_entry;
  74         _current_class_entry = _current_class_entry->next_link();
  75 
  76         if (k->is_instance_klass()) {
  77           InstanceKlass* ik = InstanceKlass::cast(k);
  78           i++;  // count all instance classes found
  79           // Not yet loaded classes are counted in max_classes
  80           // but only return loaded classes.
  81           if (ik->is_loaded()) {
  82             return ik;
  83           }
  84         }
  85       } else {
  86         // Go to next CLD
  87         if (_current_loader_data != NULL) {
  88           _current_loader_data = _current_loader_data->next();
  89         }
  90         // Start at the beginning
  91         if (_current_loader_data == NULL) {
  92           _current_loader_data = ClassLoaderDataGraph::_head;
  93         }
  94 
  95         _current_class_entry = _current_loader_data->klasses();
  96       }
  97     }
  98     // Should never be reached unless all instance classes have failed or are not fully loaded.
  99     // Caller handles NULL.
 100     return NULL;
 101   }
 102 
 103   // If the current class for the static iterator is a class being unloaded or
 104   // deallocated, adjust the current class.
 105   void adjust_saved_class(ClassLoaderData* cld) {
 106     if (_current_loader_data == cld) {
 107       _current_loader_data = cld->next();
 108       if (_current_loader_data != NULL) {
 109         _current_class_entry = _current_loader_data->klasses();
 110       }  // else try_get_next_class will start at the head
 111     }
 112   }
 113 
 114   void adjust_saved_class(Klass* klass) {
 115     if (_current_class_entry == klass) {
 116       _current_class_entry = klass->next_link();
 117     }
 118   }
 119 };
 120 
 121 static ClassLoaderDataGraphKlassIteratorStatic static_klass_iterator;
 122 
 123 InstanceKlass* ClassLoaderDataGraph::try_get_next_class() {
 124   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
 125   return static_klass_iterator.try_get_next_class();
 126 }
 127 
 128 void ClassLoaderDataGraph::adjust_saved_class(ClassLoaderData* cld) {
 129   return static_klass_iterator.adjust_saved_class(cld);
 130 }
 131 
 132 void ClassLoaderDataGraph::adjust_saved_class(Klass* klass) {
 133   return static_klass_iterator.adjust_saved_class(klass);
 134 }
 135 
 136 void ClassLoaderDataGraph::clean_deallocate_lists(bool walk_previous_versions) {
 137   assert(SafepointSynchronize::is_at_safepoint(), "must only be called at safepoint");
 138   uint loaders_processed = 0;
 139   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
 140     // is_alive check will be necessary for concurrent class unloading.
 141     if (cld->is_alive()) {
 142       // clean metaspace
 143       if (walk_previous_versions) {
 144         cld->classes_do(InstanceKlass::purge_previous_versions);
 145       }
 146       cld->free_deallocate_list();
 147       loaders_processed++;
 148     }
 149   }
 150   log_debug(class, loader, data)("clean_deallocate_lists: loaders processed %u %s",
 151                                  loaders_processed, walk_previous_versions ? "walk_previous_versions" : "");
 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 
 229 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_unsafe_anonymous) {
 230   MutexLocker ml(ClassLoaderDataGraph_lock);
 231   ClassLoaderData* loader_data = add_to_graph(loader, is_unsafe_anonymous);
 232   return loader_data;
 233 }
 234 
 235 void ClassLoaderDataGraph::cld_unloading_do(CLDClosure* cl) {
 236   assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock);
 237   // Only walk the head until any clds not purged from prior unloading
 238   // (CMS doesn't purge right away).
 239   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
 240     assert(cld->is_unloading(), "invariant");
 241     cl->do_cld(cld);
 242   }
 243 }
 244 
 245 // These are functions called by the GC, which require all of the CLDs, including the
 246 // unloading ones.
 247 void ClassLoaderDataGraph::cld_do(CLDClosure* cl) {
 248   assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock);
 249   for (ClassLoaderData* cld = _head;  cld != NULL; cld = cld->_next) {
 250     cl->do_cld(cld);
 251   }
 252 }
 253 
 254 void ClassLoaderDataGraph::roots_cld_do(CLDClosure* strong, CLDClosure* weak) {
 255   assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock);
 256   for (ClassLoaderData* cld = _head;  cld != NULL; cld = cld->_next) {
 257     CLDClosure* closure = cld->keep_alive() ? strong : weak;
 258     if (closure != NULL) {
 259       closure->do_cld(cld);
 260     }
 261   }
 262 }
 263 
 264 void ClassLoaderDataGraph::always_strong_cld_do(CLDClosure* cl) {
 265   assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock);
 266   if (ClassUnloading) {
 267     roots_cld_do(cl, NULL);
 268   } else {
 269     cld_do(cl);
 270   }
 271 }
 272 
 273 // Closure for locking and iterating through classes.
 274 LockedClassesDo::LockedClassesDo(classes_do_func_t f) : _function(f) {
 275   ClassLoaderDataGraph_lock->lock();
 276 }
 277 
 278 LockedClassesDo::LockedClassesDo() : _function(NULL) {
 279   // callers provide their own do_klass
 280   ClassLoaderDataGraph_lock->lock();
 281 }
 282 
 283 LockedClassesDo::~LockedClassesDo() { ClassLoaderDataGraph_lock->unlock(); }
 284 
 285 
 286 // Iterating over the CLDG needs to be locked because
 287 // unloading can remove entries concurrently soon.
 288 class ClassLoaderDataGraphIterator : public StackObj {
 289   ClassLoaderData* _next;
 290   HandleMark       _hm;  // clean up handles when this is done.
 291   Handle           _holder;
 292   Thread*          _thread;
 293   NoSafepointVerifier _nsv; // No safepoints allowed in this scope
 294                             // unless verifying at a safepoint.
 295 
 296 public:
 297   ClassLoaderDataGraphIterator() : _next(ClassLoaderDataGraph::_head),
 298      _nsv(true, !SafepointSynchronize::is_at_safepoint()) {
 299     _thread = Thread::current();
 300     assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 301   }
 302 
 303   ClassLoaderData* get_next() {
 304     ClassLoaderData* cld = _next;
 305     // Skip already unloaded CLD for concurrent unloading.
 306     while (cld != NULL && !cld->is_alive()) {
 307       cld = cld->next();
 308     }
 309     if (cld != NULL) {
 310       // Keep cld that is being returned alive.
 311       _holder = Handle(_thread, cld->holder_phantom());
 312       _next = cld->next();
 313     } else {
 314       _next = NULL;
 315     }
 316     return cld;
 317   }
 318 };
 319 
 320 void ClassLoaderDataGraph::loaded_cld_do(CLDClosure* cl) {
 321   ClassLoaderDataGraphIterator iter;
 322   while (ClassLoaderData* cld = iter.get_next()) {
 323     cl->do_cld(cld);
 324   }
 325 }
 326 
 327 // These functions assume that the caller has locked the ClassLoaderDataGraph_lock
 328 // if they are not calling the function from a safepoint.
 329 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) {
 330   ClassLoaderDataGraphIterator iter;
 331   while (ClassLoaderData* cld = iter.get_next()) {
 332     cld->classes_do(klass_closure);
 333   }
 334 }
 335 
 336 void ClassLoaderDataGraph::classes_do(void f(Klass* const)) {
 337   ClassLoaderDataGraphIterator iter;
 338   while (ClassLoaderData* cld = iter.get_next()) {
 339     cld->classes_do(f);
 340   }
 341 }
 342 
 343 void ClassLoaderDataGraph::methods_do(void f(Method*)) {
 344   ClassLoaderDataGraphIterator iter;
 345   while (ClassLoaderData* cld = iter.get_next()) {
 346     cld->methods_do(f);
 347   }
 348 }
 349 
 350 void ClassLoaderDataGraph::modules_do(void f(ModuleEntry*)) {
 351   assert_locked_or_safepoint(Module_lock);
 352   ClassLoaderDataGraphIterator iter;
 353   while (ClassLoaderData* cld = iter.get_next()) {
 354     cld->modules_do(f);
 355   }
 356 }
 357 
 358 void ClassLoaderDataGraph::modules_unloading_do(void f(ModuleEntry*)) {
 359   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 360   // Only walk the head until any clds not purged from prior unloading
 361   // (CMS doesn't purge right away).
 362   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
 363     assert(cld->is_unloading(), "invariant");
 364     cld->modules_do(f);
 365   }
 366 }
 367 
 368 void ClassLoaderDataGraph::packages_do(void f(PackageEntry*)) {
 369   assert_locked_or_safepoint(Module_lock);
 370   ClassLoaderDataGraphIterator iter;
 371   while (ClassLoaderData* cld = iter.get_next()) {
 372     cld->packages_do(f);
 373   }
 374 }
 375 
 376 void ClassLoaderDataGraph::packages_unloading_do(void f(PackageEntry*)) {
 377   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 378   // Only walk the head until any clds not purged from prior unloading
 379   // (CMS doesn't purge right away).
 380   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
 381     assert(cld->is_unloading(), "invariant");
 382     cld->packages_do(f);
 383   }
 384 }
 385 
 386 void ClassLoaderDataGraph::loaded_classes_do(KlassClosure* klass_closure) {
 387   ClassLoaderDataGraphIterator iter;
 388   while (ClassLoaderData* cld = iter.get_next()) {
 389     cld->loaded_classes_do(klass_closure);
 390   }
 391 }
 392 
 393 // This case can block but cannot do unloading (called from CDS)
 394 void ClassLoaderDataGraph::unlocked_loaded_classes_do(KlassClosure* klass_closure) {
 395   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
 396     cld->loaded_classes_do(klass_closure);
 397   }
 398 }
 399 
 400 
 401 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
 402   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 403   // Only walk the head until any clds not purged from prior unloading
 404   // (CMS doesn't purge right away).
 405   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
 406     assert(cld->is_unloading(), "invariant");
 407     cld->classes_do(f);
 408   }
 409 }
 410 
 411 #define FOR_ALL_DICTIONARY(X)   ClassLoaderDataGraphIterator iter; \
 412                                 while (ClassLoaderData* X = iter.get_next()) \
 413                                   if (X->dictionary() != NULL)
 414 
 415 // Walk classes in the loaded class dictionaries in various forms.
 416 // Only walks the classes defined in this class loader.
 417 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*)) {
 418   FOR_ALL_DICTIONARY(cld) {
 419     cld->dictionary()->classes_do(f);
 420   }
 421 }
 422 
 423 // Only walks the classes defined in this class loader.
 424 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*, TRAPS), TRAPS) {
 425   FOR_ALL_DICTIONARY(cld) {
 426     cld->dictionary()->classes_do(f, CHECK);
 427   }
 428 }
 429 
 430 void ClassLoaderDataGraph::verify_dictionary() {
 431   FOR_ALL_DICTIONARY(cld) {
 432     cld->dictionary()->verify();
 433   }
 434 }
 435 
 436 void ClassLoaderDataGraph::print_dictionary(outputStream* st) {
 437   FOR_ALL_DICTIONARY(cld) {
 438     st->print("Dictionary for ");
 439     cld->print_value_on(st);
 440     st->cr();
 441     cld->dictionary()->print_on(st);
 442     st->cr();
 443   }
 444 }
 445 
 446 void ClassLoaderDataGraph::print_table_statistics(outputStream* st) {
 447   FOR_ALL_DICTIONARY(cld) {
 448     ResourceMark rm;
 449     stringStream tempst;
 450     tempst.print("System Dictionary for %s class loader", cld->loader_name_and_id());
 451     cld->dictionary()->print_table_statistics(st, tempst.as_string());
 452   }
 453 }
 454 
 455 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
 456   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 457   assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
 458 
 459   GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
 460 
 461   // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
 462   ClassLoaderData* curr = _head;
 463   while (curr != _saved_head) {
 464     if (!curr->claimed()) {
 465       array->push(curr);
 466       LogTarget(Debug, class, loader, data) lt;
 467       if (lt.is_enabled()) {
 468         LogStream ls(lt);
 469         ls.print("found new CLD: ");
 470         curr->print_value_on(&ls);
 471         ls.cr();
 472       }
 473     }
 474 
 475     curr = curr->_next;
 476   }
 477 
 478   return array;
 479 }
 480 
 481 #ifndef PRODUCT
 482 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {
 483   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 484   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
 485     if (loader_data == data) {
 486       return true;
 487     }
 488   }
 489 
 490   return false;
 491 }
 492 #endif // PRODUCT
 493 
 494 bool ClassLoaderDataGraph::is_valid(ClassLoaderData* loader_data) {
 495   DEBUG_ONLY( if (!VMError::is_error_reported()) { assert_locked_or_safepoint(ClassLoaderDataGraph_lock); } )
 496   if (loader_data != NULL) {
 497     if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
 498       return true;
 499     }
 500     for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
 501       if (loader_data == data) {
 502         return true;
 503       }
 504     }
 505   }
 506   return false;
 507 }
 508 
 509 // Move class loader data from main list to the unloaded list for unloading
 510 // and deallocation later.
 511 bool ClassLoaderDataGraph::do_unloading() {
 512   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 513 
 514   // Indicate whether safepoint cleanup is needed.
 515   _safepoint_cleanup_needed = true;
 516 
 517   ClassLoaderData* data = _head;
 518   ClassLoaderData* prev = NULL;
 519   bool seen_dead_loader = false;
 520   uint loaders_processed = 0;
 521   uint loaders_removed = 0;
 522 
 523   // Save previous _unloading pointer for CMS which may add to unloading list before
 524   // purging and we don't want to rewalk the previously unloaded class loader data.
 525   _saved_unloading = _unloading;
 526 
 527   data = _head;
 528   while (data != NULL) {
 529     if (data->is_alive()) {
 530       prev = data;
 531       data = data->next();
 532       loaders_processed++;
 533       continue;
 534     }
 535     seen_dead_loader = true;
 536     loaders_removed++;
 537     ClassLoaderData* dead = data;
 538     dead->unload();
 539     data = data->next();
 540     // Remove from loader list.
 541     // This class loader data will no longer be found
 542     // in the ClassLoaderDataGraph.
 543     if (prev != NULL) {
 544       prev->set_next(data);
 545     } else {
 546       assert(dead == _head, "sanity check");
 547       _head = data;
 548     }
 549     dead->set_next(_unloading);
 550     _unloading = dead;
 551   }
 552 
 553   log_debug(class, loader, data)("do_unloading: loaders processed %u, loaders removed %u", loaders_processed, loaders_removed);
 554 
 555   return seen_dead_loader;
 556 }
 557 
 558 // There's at least one dead class loader.  Purge refererences of healthy module
 559 // reads lists and package export lists to modules belonging to dead loaders.
 560 void ClassLoaderDataGraph::clean_module_and_package_info() {
 561   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 562 
 563   ClassLoaderData* data = _head;
 564   while (data != NULL) {
 565     // Walk a ModuleEntry's reads, and a PackageEntry's exports
 566     // lists to determine if there are modules on those lists that are now
 567     // dead and should be removed.  A module's life cycle is equivalent
 568     // to its defining class loader's life cycle.  Since a module is
 569     // considered dead if its class loader is dead, these walks must
 570     // occur after each class loader's aliveness is determined.
 571     if (data->packages() != NULL) {
 572       data->packages()->purge_all_package_exports();
 573     }
 574     if (data->modules_defined()) {
 575       data->modules()->purge_all_module_reads();
 576     }
 577     data = data->next();
 578   }
 579 }
 580 
 581 void ClassLoaderDataGraph::purge() {
 582   ClassLoaderData* list = _unloading;
 583   _unloading = NULL;
 584   ClassLoaderData* next = list;
 585   bool classes_unloaded = false;
 586   while (next != NULL) {
 587     ClassLoaderData* purge_me = next;
 588     next = purge_me->next();
 589     delete purge_me;
 590     classes_unloaded = true;
 591   }
 592   if (classes_unloaded) {
 593     Metaspace::purge();
 594     set_metaspace_oom(false);
 595   }
 596   DependencyContext::purge_dependency_contexts();
 597 }
 598 
 599 int ClassLoaderDataGraph::resize_dictionaries() {
 600   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 601   int resized = 0;
 602   assert (Dictionary::does_any_dictionary_needs_resizing(), "some dictionary should need resizing");
 603   FOR_ALL_DICTIONARY(cld) {
 604     if (cld->dictionary()->resize_if_needed()) {
 605       resized++;
 606     }
 607   }
 608   return resized;
 609 }
 610 
 611 ClassLoaderDataGraphKlassIteratorAtomic::ClassLoaderDataGraphKlassIteratorAtomic()
 612     : _next_klass(NULL) {
 613   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 614   ClassLoaderData* cld = ClassLoaderDataGraph::_head;
 615   Klass* klass = NULL;
 616 
 617   // Find the first klass in the CLDG.
 618   while (cld != NULL) {
 619     assert_locked_or_safepoint(cld->metaspace_lock());
 620     klass = cld->_klasses;
 621     if (klass != NULL) {
 622       _next_klass = klass;
 623       return;
 624     }
 625     cld = cld->next();
 626   }
 627 }
 628 
 629 Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass_in_cldg(Klass* klass) {
 630   Klass* next = klass->next_link();
 631   if (next != NULL) {
 632     return next;
 633   }
 634 
 635   // No more klasses in the current CLD. Time to find a new CLD.
 636   ClassLoaderData* cld = klass->class_loader_data();
 637   assert_locked_or_safepoint(cld->metaspace_lock());
 638   while (next == NULL) {
 639     cld = cld->next();
 640     if (cld == NULL) {
 641       break;
 642     }
 643     next = cld->_klasses;
 644   }
 645 
 646   return next;
 647 }
 648 
 649 Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass() {
 650   Klass* head = _next_klass;
 651 
 652   while (head != NULL) {
 653     Klass* next = next_klass_in_cldg(head);
 654 
 655     Klass* old_head = Atomic::cmpxchg(next, &_next_klass, head);
 656 
 657     if (old_head == head) {
 658       return head; // Won the CAS.
 659     }
 660 
 661     head = old_head;
 662   }
 663 
 664   // Nothing more for the iterator to hand out.
 665   assert(head == NULL, "head is " PTR_FORMAT ", expected not null:", p2i(head));
 666   return NULL;
 667 }
 668 
 669 ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() {
 670   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 671   _data = ClassLoaderDataGraph::_head;
 672 }
 673 
 674 ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {}
 675 
 676 ClassLoaderMetaspace* ClassLoaderDataGraphMetaspaceIterator::get_next() {
 677   assert(_data != NULL, "Should not be NULL in call to the iterator");
 678   ClassLoaderMetaspace* result = _data->metaspace_or_null();
 679   _data = _data->next();
 680   // This result might be NULL for class loaders without metaspace
 681   // yet.  It would be nice to return only non-null results but
 682   // there is no guarantee that there will be a non-null result
 683   // down the list so the caller is going to have to check.
 684   return result;
 685 }
 686 
 687 #ifndef PRODUCT
 688 // callable from debugger
 689 extern "C" int print_loader_data_graph() {
 690   ResourceMark rm;
 691   ClassLoaderDataGraph::print_on(tty);
 692   return 0;
 693 }
 694 
 695 void ClassLoaderDataGraph::verify() {
 696   ClassLoaderDataGraphIterator iter;
 697   while (ClassLoaderData* cld = iter.get_next()) {
 698     cld->verify();
 699   }
 700 }
 701 
 702 void ClassLoaderDataGraph::print_on(outputStream * const out) {
 703   ClassLoaderDataGraphIterator iter;
 704   while (ClassLoaderData* cld = iter.get_next()) {
 705     cld->print_on(out);
 706   }
 707 }
 708 #endif // PRODUCT
 709 
 710 void ClassLoaderDataGraph::print() { print_on(tty); }