1 /*
   2  * Copyright (c) 2018, 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 "logging/log.hpp"
  33 #include "logging/logStream.hpp"
  34 #include "memory/allocation.inline.hpp"
  35 #include "memory/metaspace.hpp"
  36 #include "memory/resourceArea.hpp"
  37 #include "runtime/atomic.hpp"
  38 #include "runtime/handles.inline.hpp"
  39 #include "runtime/mutex.hpp"
  40 #include "runtime/safepoint.hpp"
  41 #include "runtime/safepointVerifiers.hpp"
  42 #include "utilities/growableArray.hpp"
  43 #include "utilities/macros.hpp"
  44 #include "utilities/ostream.hpp"
  45 
  46 volatile size_t ClassLoaderDataGraph::_num_array_classes = 0;
  47 volatile size_t ClassLoaderDataGraph::_num_instance_classes = 0;
  48 
  49 void ClassLoaderDataGraph::clear_claimed_marks() {
  50   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
  51     cld->clear_claim();
  52   }
  53 }
  54 
  55 // Class iterator used by the compiler.  It gets some number of classes at
  56 // a safepoint to decay invocation counters on the methods.
  57 class ClassLoaderDataGraphKlassIteratorStatic {
  58   ClassLoaderData* _current_loader_data;
  59   Klass*           _current_class_entry;
  60  public:
  61 
  62   ClassLoaderDataGraphKlassIteratorStatic() : _current_loader_data(NULL), _current_class_entry(NULL) {}
  63 
  64   InstanceKlass* try_get_next_class() {
  65     assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
  66     size_t max_classes = ClassLoaderDataGraph::num_instance_classes();
  67     assert(max_classes > 0, "should not be called with no instance classes");
  68     for (size_t i = 0; i < max_classes; ) {
  69 
  70       if (_current_class_entry != NULL) {
  71         Klass* k = _current_class_entry;
  72         _current_class_entry = _current_class_entry->next_link();
  73 
  74         if (k->is_instance_klass()) {
  75           InstanceKlass* ik = InstanceKlass::cast(k);
  76           i++;  // count all instance classes found
  77           // Not yet loaded classes are counted in max_classes
  78           // but only return loaded classes.
  79           if (ik->is_loaded()) {
  80             return ik;
  81           }
  82         }
  83       } else {
  84         // Go to next CLD
  85         if (_current_loader_data != NULL) {
  86           _current_loader_data = _current_loader_data->next();
  87         }
  88         // Start at the beginning
  89         if (_current_loader_data == NULL) {
  90           _current_loader_data = ClassLoaderDataGraph::_head;
  91         }
  92 
  93         _current_class_entry = _current_loader_data->klasses();
  94       }
  95     }
  96     // Should never be reached unless all instance classes have failed or are not fully loaded.
  97     // Caller handles NULL.
  98     return NULL;
  99   }
 100 
 101   // If the current class for the static iterator is a class being unloaded or
 102   // deallocated, adjust the current class.
 103   void adjust_saved_class(ClassLoaderData* cld) {
 104     if (_current_loader_data == cld) {
 105       _current_loader_data = cld->next();
 106       if (_current_loader_data != NULL) {
 107         _current_class_entry = _current_loader_data->klasses();
 108       }  // else try_get_next_class will start at the head
 109     }
 110   }
 111 
 112   void adjust_saved_class(Klass* klass) {
 113     if (_current_class_entry == klass) {
 114       _current_class_entry = klass->next_link();
 115     }
 116   }
 117 };
 118 
 119 static ClassLoaderDataGraphKlassIteratorStatic static_klass_iterator;
 120 
 121 InstanceKlass* ClassLoaderDataGraph::try_get_next_class() {
 122   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
 123   return static_klass_iterator.try_get_next_class();
 124 }
 125 
 126 void ClassLoaderDataGraph::adjust_saved_class(ClassLoaderData* cld) {
 127   return static_klass_iterator.adjust_saved_class(cld);
 128 }
 129 
 130 void ClassLoaderDataGraph::adjust_saved_class(Klass* klass) {
 131   return static_klass_iterator.adjust_saved_class(klass);
 132 }
 133 
 134 void ClassLoaderDataGraph::clean_deallocate_lists(bool walk_previous_versions) {
 135   assert(SafepointSynchronize::is_at_safepoint(), "must only be called at safepoint");
 136   uint loaders_processed = 0;
 137   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
 138     // is_alive check will be necessary for concurrent class unloading.
 139     if (cld->is_alive()) {
 140       // clean metaspace
 141       if (walk_previous_versions) {
 142         cld->classes_do(InstanceKlass::purge_previous_versions);
 143       }
 144       cld->free_deallocate_list();
 145       loaders_processed++;
 146     }
 147   }
 148   log_debug(class, loader, data)("clean_deallocate_lists: loaders processed %u %s",
 149                                  loaders_processed, walk_previous_versions ? "walk_previous_versions" : "");
 150 }
 151 
 152 void ClassLoaderDataGraph::walk_metadata_and_clean_metaspaces() {
 153   assert(SafepointSynchronize::is_at_safepoint(), "must only be called at safepoint");
 154 
 155   _should_clean_deallocate_lists = false; // assume everything gets cleaned
 156 
 157   // Mark metadata seen on the stack so we can delete unreferenced entries.
 158   // Walk all metadata, including the expensive code cache walk, only for class redefinition.
 159   // The MetadataOnStackMark walk during redefinition saves previous versions if it finds old methods
 160   // on the stack or in the code cache, so we only have to repeat the full walk if
 161   // they were found at that time.
 162   // TODO: have redefinition clean old methods out of the code cache.  They still exist in some places.
 163   bool walk_all_metadata = InstanceKlass::has_previous_versions_and_reset();
 164 
 165   MetadataOnStackMark md_on_stack(walk_all_metadata);
 166   clean_deallocate_lists(walk_all_metadata);
 167 }
 168 
 169 // GC root of class loader data created.
 170 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
 171 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
 172 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;
 173 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
 174 
 175 bool ClassLoaderDataGraph::_should_purge = false;
 176 bool ClassLoaderDataGraph::_should_clean_deallocate_lists = false;
 177 bool ClassLoaderDataGraph::_safepoint_cleanup_needed = false;
 178 bool ClassLoaderDataGraph::_metaspace_oom = false;
 179 
 180 // Add a new class loader data node to the list.  Assign the newly created
 181 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
 182 ClassLoaderData* ClassLoaderDataGraph::add_to_graph(Handle loader, bool is_unsafe_anonymous) {
 183 
 184   assert_lock_strong(ClassLoaderDataGraph_lock);
 185 
 186   ClassLoaderData* cld;
 187 
 188   // First check if another thread beat us to creating the CLD and installing
 189   // it into the loader while we were waiting for the lock.
 190   if (!is_unsafe_anonymous && loader.not_null()) {
 191     cld = java_lang_ClassLoader::loader_data_acquire(loader());
 192     if (cld != NULL) {
 193       return cld;
 194     }
 195   }
 196 
 197   // We mustn't GC until we've installed the ClassLoaderData in the Graph since the CLD
 198   // contains oops in _handles that must be walked.  GC doesn't walk CLD from the
 199   // loader oop in all collections, particularly young collections.
 200   NoSafepointVerifier no_safepoints;
 201 
 202   cld = new ClassLoaderData(loader, is_unsafe_anonymous);
 203 
 204   // First install the new CLD to the Graph.
 205   cld->set_next(_head);
 206   _head = cld;
 207 
 208   // Next associate with the class_loader.
 209   if (!is_unsafe_anonymous) {
 210     // Use OrderAccess, since readers need to get the loader_data only after
 211     // it's added to the Graph
 212     java_lang_ClassLoader::release_set_loader_data(loader(), cld);
 213   }
 214 
 215   // Lastly log, if requested
 216   LogTarget(Trace, class, loader, data) lt;
 217   if (lt.is_enabled()) {
 218     ResourceMark rm;
 219     LogStream ls(lt);
 220     ls.print("create ");
 221     cld->print_value_on(&ls);
 222     ls.cr();
 223   }
 224   return cld;
 225 }
 226 
 227 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_unsafe_anonymous) {
 228   MutexLocker ml(ClassLoaderDataGraph_lock);
 229   ClassLoaderData* loader_data = add_to_graph(loader, is_unsafe_anonymous);
 230   return loader_data;
 231 }
 232 
 233 void ClassLoaderDataGraph::cld_do(CLDClosure* cl) {
 234   assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock);
 235   for (ClassLoaderData* cld = _head;  cld != NULL; cld = cld->_next) {
 236     cl->do_cld(cld);
 237   }
 238 }
 239 
 240 void ClassLoaderDataGraph::cld_unloading_do(CLDClosure* cl) {
 241   assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock);
 242   // Only walk the head until any clds not purged from prior unloading
 243   // (CMS doesn't purge right away).
 244   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
 245     assert(cld->is_unloading(), "invariant");
 246     cl->do_cld(cld);
 247   }
 248 }
 249 
 250 void ClassLoaderDataGraph::roots_cld_do(CLDClosure* strong, CLDClosure* weak) {
 251   assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock);
 252   for (ClassLoaderData* cld = _head;  cld != NULL; cld = cld->_next) {
 253     CLDClosure* closure = cld->keep_alive() ? strong : weak;
 254     if (closure != NULL) {
 255       closure->do_cld(cld);
 256     }
 257   }
 258 }
 259 
 260 void ClassLoaderDataGraph::always_strong_cld_do(CLDClosure* cl) {
 261   assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock);
 262   if (ClassUnloading) {
 263     roots_cld_do(cl, NULL);
 264   } else {
 265     cld_do(cl);
 266   }
 267 }
 268 
 269 // Closure for locking and iterating through classes.
 270 LockedClassesDo::LockedClassesDo(classes_do_func_t f) : _function(f) {
 271   ClassLoaderDataGraph_lock->lock();
 272 }
 273 
 274 LockedClassesDo::LockedClassesDo() : _function(NULL) {
 275   // callers provide their own do_klass
 276   ClassLoaderDataGraph_lock->lock();
 277 }
 278 
 279 LockedClassesDo::~LockedClassesDo() { ClassLoaderDataGraph_lock->unlock(); }
 280 
 281 
 282 // Iterating over the CLDG needs to be locked because
 283 // unloading can remove entries concurrently soon.
 284 class ClassLoaderDataGraphIterator : public StackObj {
 285   ClassLoaderData* _next;
 286   HandleMark       _hm;  // clean up handles when this is done.
 287   Handle           _holder;
 288   Thread*          _thread;
 289 
 290 public:
 291   ClassLoaderDataGraphIterator() : _next(ClassLoaderDataGraph::_head) {
 292     _thread = Thread::current();
 293     assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 294   }
 295 
 296   ClassLoaderData* get_next() {
 297     ClassLoaderData* cld = _next;
 298     // Skip already unloaded CLD for concurrent unloading.
 299     while (cld != NULL && !cld->is_alive()) {
 300       cld = cld->next();
 301     }
 302     if (cld != NULL) {
 303       // Keep cld that is being returned alive.
 304       _holder = Handle(_thread, cld->holder_phantom());
 305       _next = cld->next();
 306     } else {
 307       _next = NULL;
 308     }
 309     return cld;
 310   }
 311 
 312 
 313 };
 314 
 315 // These functions assume that the caller has locked the ClassLoaderDataGraph_lock
 316 // if they are not calling the function from a safepoint.
 317 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) {
 318   ClassLoaderDataGraphIterator iter;
 319   while (ClassLoaderData* cld = iter.get_next()) {
 320     cld->classes_do(klass_closure);
 321   }
 322 }
 323 
 324 void ClassLoaderDataGraph::classes_do(void f(Klass* const)) {
 325   ClassLoaderDataGraphIterator iter;
 326   while (ClassLoaderData* cld = iter.get_next()) {
 327     cld->classes_do(f);
 328   }
 329 }
 330 
 331 void ClassLoaderDataGraph::methods_do(void f(Method*)) {
 332   ClassLoaderDataGraphIterator iter;
 333   while (ClassLoaderData* cld = iter.get_next()) {
 334     cld->methods_do(f);
 335   }
 336 }
 337 
 338 void ClassLoaderDataGraph::modules_do(void f(ModuleEntry*)) {
 339   assert_locked_or_safepoint(Module_lock);
 340   ClassLoaderDataGraphIterator iter;
 341   while (ClassLoaderData* cld = iter.get_next()) {
 342     cld->modules_do(f);
 343   }
 344 }
 345 
 346 void ClassLoaderDataGraph::modules_unloading_do(void f(ModuleEntry*)) {
 347   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 348   // Only walk the head until any clds not purged from prior unloading
 349   // (CMS doesn't purge right away).
 350   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
 351     assert(cld->is_unloading(), "invariant");
 352     cld->modules_do(f);
 353   }
 354 }
 355 
 356 void ClassLoaderDataGraph::packages_do(void f(PackageEntry*)) {
 357   assert_locked_or_safepoint(Module_lock);
 358   ClassLoaderDataGraphIterator iter;
 359   while (ClassLoaderData* cld = iter.get_next()) {
 360     cld->packages_do(f);
 361   }
 362 }
 363 
 364 void ClassLoaderDataGraph::packages_unloading_do(void f(PackageEntry*)) {
 365   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 366   // Only walk the head until any clds not purged from prior unloading
 367   // (CMS doesn't purge right away).
 368   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
 369     assert(cld->is_unloading(), "invariant");
 370     cld->packages_do(f);
 371   }
 372 }
 373 
 374 void ClassLoaderDataGraph::loaded_classes_do(KlassClosure* klass_closure) {
 375   ClassLoaderDataGraphIterator iter;
 376   while (ClassLoaderData* cld = iter.get_next()) {
 377     cld->loaded_classes_do(klass_closure);
 378   }
 379 }
 380 
 381 // This case can block but cannot do unloading (called from CDS)
 382 void ClassLoaderDataGraph::unlocked_loaded_classes_do(KlassClosure* klass_closure) {
 383   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
 384     cld->loaded_classes_do(klass_closure);
 385   }
 386 }
 387 
 388 
 389 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
 390   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 391   // Only walk the head until any clds not purged from prior unloading
 392   // (CMS doesn't purge right away).
 393   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
 394     assert(cld->is_unloading(), "invariant");
 395     cld->classes_do(f);
 396   }
 397 }
 398 
 399 #define FOR_ALL_DICTIONARY(X)   ClassLoaderDataGraphIterator iter; \
 400                                 while (ClassLoaderData* X = iter.get_next()) \
 401                                   if (X->dictionary() != NULL)
 402 
 403 // Walk classes in the loaded class dictionaries in various forms.
 404 // Only walks the classes defined in this class loader.
 405 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*)) {
 406   FOR_ALL_DICTIONARY(cld) {
 407     cld->dictionary()->classes_do(f);
 408   }
 409 }
 410 
 411 // Only walks the classes defined in this class loader.
 412 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*, TRAPS), TRAPS) {
 413   FOR_ALL_DICTIONARY(cld) {
 414     cld->dictionary()->classes_do(f, CHECK);
 415   }
 416 }
 417 
 418 void ClassLoaderDataGraph::verify_dictionary() {
 419   FOR_ALL_DICTIONARY(cld) {
 420     cld->dictionary()->verify();
 421   }
 422 }
 423 
 424 void ClassLoaderDataGraph::print_dictionary(outputStream* st) {
 425   FOR_ALL_DICTIONARY(cld) {
 426     st->print("Dictionary for ");
 427     cld->print_value_on(st);
 428     st->cr();
 429     cld->dictionary()->print_on(st);
 430     st->cr();
 431   }
 432 }
 433 
 434 void ClassLoaderDataGraph::print_dictionary_statistics(outputStream* st) {
 435   FOR_ALL_DICTIONARY(cld) {
 436     ResourceMark rm;
 437     stringStream tempst;
 438     tempst.print("System Dictionary for %s class loader", cld->loader_name_and_id());
 439     cld->dictionary()->print_table_statistics(st, tempst.as_string());
 440   }
 441 }
 442 
 443 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
 444   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 445   assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
 446 
 447   GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
 448 
 449   // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
 450   ClassLoaderData* curr = _head;
 451   while (curr != _saved_head) {
 452     if (!curr->claimed()) {
 453       array->push(curr);
 454       LogTarget(Debug, class, loader, data) lt;
 455       if (lt.is_enabled()) {
 456         LogStream ls(lt);
 457         ls.print("found new CLD: ");
 458         curr->print_value_on(&ls);
 459         ls.cr();
 460       }
 461     }
 462 
 463     curr = curr->_next;
 464   }
 465 
 466   return array;
 467 }
 468 
 469 #ifndef PRODUCT
 470 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {
 471   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 472   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
 473     if (loader_data == data) {
 474       return true;
 475     }
 476   }
 477 
 478   return false;
 479 }
 480 #endif // PRODUCT
 481 
 482 bool ClassLoaderDataGraph::is_valid(ClassLoaderData* loader_data) {
 483   DEBUG_ONLY( if (!VMError::is_error_reported()) { assert_locked_or_safepoint(ClassLoaderDataGraph_lock); } )
 484   if (loader_data != NULL) {
 485     if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
 486       return true;
 487     }
 488     for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
 489       if (loader_data == data) {
 490         return true;
 491       }
 492     }
 493   }
 494   return false;
 495 }
 496 
 497 // Move class loader data from main list to the unloaded list for unloading
 498 // and deallocation later.
 499 bool ClassLoaderDataGraph::do_unloading(bool do_cleaning) {
 500   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 501 
 502   // Indicate whether safepoint cleanup is needed.
 503   _safepoint_cleanup_needed |= do_cleaning;
 504 
 505   ClassLoaderData* data = _head;
 506   ClassLoaderData* prev = NULL;
 507   bool seen_dead_loader = false;
 508   uint loaders_processed = 0;
 509   uint loaders_removed = 0;
 510 
 511   // Save previous _unloading pointer for CMS which may add to unloading list before
 512   // purging and we don't want to rewalk the previously unloaded class loader data.
 513   _saved_unloading = _unloading;
 514 
 515   data = _head;
 516   while (data != NULL) {
 517     if (data->is_alive()) {
 518       prev = data;
 519       data = data->next();
 520       loaders_processed++;
 521       continue;
 522     }
 523     seen_dead_loader = true;
 524     loaders_removed++;
 525     ClassLoaderData* dead = data;
 526     dead->unload();
 527     data = data->next();
 528     // Remove from loader list.
 529     // This class loader data will no longer be found
 530     // in the ClassLoaderDataGraph.
 531     if (prev != NULL) {
 532       prev->set_next(data);
 533     } else {
 534       assert(dead == _head, "sanity check");
 535       _head = data;
 536     }
 537     dead->set_next(_unloading);
 538     _unloading = dead;
 539   }
 540 
 541   log_debug(class, loader, data)("do_unloading: loaders processed %u, loaders removed %u", loaders_processed, loaders_removed);
 542 
 543   return seen_dead_loader;
 544 }
 545 
 546 // There's at least one dead class loader.  Purge refererences of healthy module
 547 // reads lists and package export lists to modules belonging to dead loaders.
 548 void ClassLoaderDataGraph::clean_module_and_package_info() {
 549   assert_locked_or_safepoint(ClassLoaderDataGraph_lock);
 550 
 551   ClassLoaderData* data = _head;
 552   while (data != NULL) {
 553     // Remove entries in the dictionary of live class loader that have
 554     // initiated loading classes in a dead class loader.
 555     if (data->dictionary() != NULL) {
 556       data->dictionary()->do_unloading();
 557     }
 558     // Walk a ModuleEntry's reads, and a PackageEntry's exports
 559     // lists to determine if there are modules on those lists that are now
 560     // dead and should be removed.  A module's life cycle is equivalent
 561     // to its defining class loader's life cycle.  Since a module is
 562     // considered dead if its class loader is dead, these walks must
 563     // occur after each class loader's aliveness is determined.
 564     if (data->packages() != NULL) {
 565       data->packages()->purge_all_package_exports();
 566     }
 567     if (data->modules_defined()) {
 568       data->modules()->purge_all_module_reads();
 569     }
 570     data = data->next();
 571   }
 572 }
 573 
 574 void ClassLoaderDataGraph::purge() {
 575   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 576   ClassLoaderData* list = _unloading;
 577   _unloading = NULL;
 578   ClassLoaderData* next = list;
 579   bool classes_unloaded = false;
 580   while (next != NULL) {
 581     ClassLoaderData* purge_me = next;
 582     next = purge_me->next();
 583     delete purge_me;
 584     classes_unloaded = true;
 585   }
 586   if (classes_unloaded) {
 587     Metaspace::purge();
 588     set_metaspace_oom(false);
 589   }
 590 }
 591 
 592 int ClassLoaderDataGraph::resize_if_needed() {
 593   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 594   int resized = 0;
 595   if (Dictionary::does_any_dictionary_needs_resizing()) {
 596     FOR_ALL_DICTIONARY(cld) {
 597       if (cld->dictionary()->resize_if_needed()) {
 598         resized++;
 599       }
 600     }
 601   }
 602   return resized;
 603 }
 604 
 605 ClassLoaderDataGraphKlassIteratorAtomic::ClassLoaderDataGraphKlassIteratorAtomic()
 606     : _next_klass(NULL) {
 607   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 608   ClassLoaderData* cld = ClassLoaderDataGraph::_head;
 609   Klass* klass = NULL;
 610 
 611   // Find the first klass in the CLDG.
 612   while (cld != NULL) {
 613     assert_locked_or_safepoint(cld->metaspace_lock());
 614     klass = cld->_klasses;
 615     if (klass != NULL) {
 616       _next_klass = klass;
 617       return;
 618     }
 619     cld = cld->next();
 620   }
 621 }
 622 
 623 Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass_in_cldg(Klass* klass) {
 624   Klass* next = klass->next_link();
 625   if (next != NULL) {
 626     return next;
 627   }
 628 
 629   // No more klasses in the current CLD. Time to find a new CLD.
 630   ClassLoaderData* cld = klass->class_loader_data();
 631   assert_locked_or_safepoint(cld->metaspace_lock());
 632   while (next == NULL) {
 633     cld = cld->next();
 634     if (cld == NULL) {
 635       break;
 636     }
 637     next = cld->_klasses;
 638   }
 639 
 640   return next;
 641 }
 642 
 643 Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass() {
 644   Klass* head = _next_klass;
 645 
 646   while (head != NULL) {
 647     Klass* next = next_klass_in_cldg(head);
 648 
 649     Klass* old_head = Atomic::cmpxchg(next, &_next_klass, head);
 650 
 651     if (old_head == head) {
 652       return head; // Won the CAS.
 653     }
 654 
 655     head = old_head;
 656   }
 657 
 658   // Nothing more for the iterator to hand out.
 659   assert(head == NULL, "head is " PTR_FORMAT ", expected not null:", p2i(head));
 660   return NULL;
 661 }
 662 
 663 ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() {
 664   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 665   _data = ClassLoaderDataGraph::_head;
 666 }
 667 
 668 ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {}
 669 
 670 ClassLoaderMetaspace* ClassLoaderDataGraphMetaspaceIterator::get_next() {
 671   assert(_data != NULL, "Should not be NULL in call to the iterator");
 672   ClassLoaderMetaspace* result = _data->metaspace_or_null();
 673   _data = _data->next();
 674   // This result might be NULL for class loaders without metaspace
 675   // yet.  It would be nice to return only non-null results but
 676   // there is no guarantee that there will be a non-null result
 677   // down the list so the caller is going to have to check.
 678   return result;
 679 }
 680 
 681 #ifndef PRODUCT
 682 // callable from debugger
 683 extern "C" int print_loader_data_graph() {
 684   ResourceMark rm;
 685   ClassLoaderDataGraph::print_on(tty);
 686   return 0;
 687 }
 688 
 689 void ClassLoaderDataGraph::verify() {
 690   ClassLoaderDataGraphIterator iter;
 691   while (ClassLoaderData* cld = iter.get_next()) {
 692     cld->verify();
 693   }
 694 }
 695 
 696 void ClassLoaderDataGraph::print_on(outputStream * const out) {
 697   ClassLoaderDataGraphIterator iter;
 698   while (ClassLoaderData* cld = iter.get_next()) {
 699     cld->print_on(out);
 700   }
 701 }
 702 #endif // PRODUCT