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_unloading_do(CLDClosure* cl) { 234 assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock); 235 // Only walk the head until any clds not purged from prior unloading 236 // (CMS doesn't purge right away). 237 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) { 238 assert(cld->is_unloading(), "invariant"); 239 cl->do_cld(cld); 240 } 241 } 242 243 // These are functions called by the GC, which require all of the CLDs, including the 244 // unloading ones. 245 void ClassLoaderDataGraph::cld_do(CLDClosure* cl) { 246 assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock); 247 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->_next) { 248 cl->do_cld(cld); 249 } 250 } 251 252 void ClassLoaderDataGraph::roots_cld_do(CLDClosure* strong, CLDClosure* weak) { 253 assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock); 254 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->_next) { 255 CLDClosure* closure = cld->keep_alive() ? strong : weak; 256 if (closure != NULL) { 257 closure->do_cld(cld); 258 } 259 } 260 } 261 262 void ClassLoaderDataGraph::always_strong_cld_do(CLDClosure* cl) { 263 assert_locked_or_safepoint_weak(ClassLoaderDataGraph_lock); 264 if (ClassUnloading) { 265 roots_cld_do(cl, NULL); 266 } else { 267 cld_do(cl); 268 } 269 } 270 271 // Closure for locking and iterating through classes. 272 LockedClassesDo::LockedClassesDo(classes_do_func_t f) : _function(f) { 273 ClassLoaderDataGraph_lock->lock(); 274 } 275 276 LockedClassesDo::LockedClassesDo() : _function(NULL) { 277 // callers provide their own do_klass 278 ClassLoaderDataGraph_lock->lock(); 279 } 280 281 LockedClassesDo::~LockedClassesDo() { ClassLoaderDataGraph_lock->unlock(); } 282 283 284 // Iterating over the CLDG needs to be locked because 285 // unloading can remove entries concurrently soon. 286 class ClassLoaderDataGraphIterator : public StackObj { 287 ClassLoaderData* _next; 288 HandleMark _hm; // clean up handles when this is done. 289 Handle _holder; 290 Thread* _thread; 291 NoSafepointVerifier _nsv; // No safepoints allowed in this scope 292 // unless verifying at a safepoint. 293 294 public: 295 ClassLoaderDataGraphIterator() : _next(ClassLoaderDataGraph::_head), 296 _nsv(true, !SafepointSynchronize::is_at_safepoint()) { 297 _thread = Thread::current(); 298 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); 299 } 300 301 ClassLoaderData* get_next() { 302 ClassLoaderData* cld = _next; 303 // Skip already unloaded CLD for concurrent unloading. 304 while (cld != NULL && !cld->is_alive()) { 305 cld = cld->next(); 306 } 307 if (cld != NULL) { 308 // Keep cld that is being returned alive. 309 _holder = Handle(_thread, cld->holder_phantom()); 310 _next = cld->next(); 311 } else { 312 _next = NULL; 313 } 314 return cld; 315 } 316 }; 317 318 void ClassLoaderDataGraph::loaded_cld_do(CLDClosure* cl) { 319 ClassLoaderDataGraphIterator iter; 320 while (ClassLoaderData* cld = iter.get_next()) { 321 cl->do_cld(cld); 322 } 323 } 324 325 // These functions assume that the caller has locked the ClassLoaderDataGraph_lock 326 // if they are not calling the function from a safepoint. 327 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) { 328 ClassLoaderDataGraphIterator iter; 329 while (ClassLoaderData* cld = iter.get_next()) { 330 cld->classes_do(klass_closure); 331 } 332 } 333 334 void ClassLoaderDataGraph::classes_do(void f(Klass* const)) { 335 ClassLoaderDataGraphIterator iter; 336 while (ClassLoaderData* cld = iter.get_next()) { 337 cld->classes_do(f); 338 } 339 } 340 341 void ClassLoaderDataGraph::methods_do(void f(Method*)) { 342 ClassLoaderDataGraphIterator iter; 343 while (ClassLoaderData* cld = iter.get_next()) { 344 cld->methods_do(f); 345 } 346 } 347 348 void ClassLoaderDataGraph::modules_do(void f(ModuleEntry*)) { 349 assert_locked_or_safepoint(Module_lock); 350 ClassLoaderDataGraphIterator iter; 351 while (ClassLoaderData* cld = iter.get_next()) { 352 cld->modules_do(f); 353 } 354 } 355 356 void ClassLoaderDataGraph::modules_unloading_do(void f(ModuleEntry*)) { 357 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); 358 // Only walk the head until any clds not purged from prior unloading 359 // (CMS doesn't purge right away). 360 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) { 361 assert(cld->is_unloading(), "invariant"); 362 cld->modules_do(f); 363 } 364 } 365 366 void ClassLoaderDataGraph::packages_do(void f(PackageEntry*)) { 367 assert_locked_or_safepoint(Module_lock); 368 ClassLoaderDataGraphIterator iter; 369 while (ClassLoaderData* cld = iter.get_next()) { 370 cld->packages_do(f); 371 } 372 } 373 374 void ClassLoaderDataGraph::packages_unloading_do(void f(PackageEntry*)) { 375 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); 376 // Only walk the head until any clds not purged from prior unloading 377 // (CMS doesn't purge right away). 378 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) { 379 assert(cld->is_unloading(), "invariant"); 380 cld->packages_do(f); 381 } 382 } 383 384 void ClassLoaderDataGraph::loaded_classes_do(KlassClosure* klass_closure) { 385 ClassLoaderDataGraphIterator iter; 386 while (ClassLoaderData* cld = iter.get_next()) { 387 cld->loaded_classes_do(klass_closure); 388 } 389 } 390 391 // This case can block but cannot do unloading (called from CDS) 392 void ClassLoaderDataGraph::unlocked_loaded_classes_do(KlassClosure* klass_closure) { 393 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { 394 cld->loaded_classes_do(klass_closure); 395 } 396 } 397 398 399 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) { 400 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); 401 // Only walk the head until any clds not purged from prior unloading 402 // (CMS doesn't purge right away). 403 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) { 404 assert(cld->is_unloading(), "invariant"); 405 cld->classes_do(f); 406 } 407 } 408 409 #define FOR_ALL_DICTIONARY(X) ClassLoaderDataGraphIterator iter; \ 410 while (ClassLoaderData* X = iter.get_next()) \ 411 if (X->dictionary() != NULL) 412 413 // Walk classes in the loaded class dictionaries in various forms. 414 // Only walks the classes defined in this class loader. 415 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*)) { 416 FOR_ALL_DICTIONARY(cld) { 417 cld->dictionary()->classes_do(f); 418 } 419 } 420 421 // Only walks the classes defined in this class loader. 422 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*, TRAPS), TRAPS) { 423 FOR_ALL_DICTIONARY(cld) { 424 cld->dictionary()->classes_do(f, CHECK); 425 } 426 } 427 428 void ClassLoaderDataGraph::verify_dictionary() { 429 FOR_ALL_DICTIONARY(cld) { 430 cld->dictionary()->verify(); 431 } 432 } 433 434 void ClassLoaderDataGraph::print_dictionary(outputStream* st) { 435 FOR_ALL_DICTIONARY(cld) { 436 st->print("Dictionary for "); 437 cld->print_value_on(st); 438 st->cr(); 439 cld->dictionary()->print_on(st); 440 st->cr(); 441 } 442 } 443 444 void ClassLoaderDataGraph::print_dictionary_statistics(outputStream* st) { 445 FOR_ALL_DICTIONARY(cld) { 446 ResourceMark rm; 447 stringStream tempst; 448 tempst.print("System Dictionary for %s class loader", cld->loader_name_and_id()); 449 cld->dictionary()->print_table_statistics(st, tempst.as_string()); 450 } 451 } 452 453 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() { 454 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); 455 assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?"); 456 457 GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>(); 458 459 // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true); 460 ClassLoaderData* curr = _head; 461 while (curr != _saved_head) { 462 if (!curr->claimed()) { 463 array->push(curr); 464 LogTarget(Debug, class, loader, data) lt; 465 if (lt.is_enabled()) { 466 LogStream ls(lt); 467 ls.print("found new CLD: "); 468 curr->print_value_on(&ls); 469 ls.cr(); 470 } 471 } 472 473 curr = curr->_next; 474 } 475 476 return array; 477 } 478 479 #ifndef PRODUCT 480 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) { 481 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); 482 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) { 483 if (loader_data == data) { 484 return true; 485 } 486 } 487 488 return false; 489 } 490 #endif // PRODUCT 491 492 bool ClassLoaderDataGraph::is_valid(ClassLoaderData* loader_data) { 493 DEBUG_ONLY( if (!VMError::is_error_reported()) { assert_locked_or_safepoint(ClassLoaderDataGraph_lock); } ) 494 if (loader_data != NULL) { 495 if (loader_data == ClassLoaderData::the_null_class_loader_data()) { 496 return true; 497 } 498 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) { 499 if (loader_data == data) { 500 return true; 501 } 502 } 503 } 504 return false; 505 } 506 507 // Move class loader data from main list to the unloaded list for unloading 508 // and deallocation later. 509 bool ClassLoaderDataGraph::do_unloading(bool do_cleaning) { 510 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); 511 512 // Indicate whether safepoint cleanup is needed. 513 _safepoint_cleanup_needed |= do_cleaning; 514 515 ClassLoaderData* data = _head; 516 ClassLoaderData* prev = NULL; 517 bool seen_dead_loader = false; 518 uint loaders_processed = 0; 519 uint loaders_removed = 0; 520 521 // Save previous _unloading pointer for CMS which may add to unloading list before 522 // purging and we don't want to rewalk the previously unloaded class loader data. 523 _saved_unloading = _unloading; 524 525 data = _head; 526 while (data != NULL) { 527 if (data->is_alive()) { 528 prev = data; 529 data = data->next(); 530 loaders_processed++; 531 continue; 532 } 533 seen_dead_loader = true; 534 loaders_removed++; 535 ClassLoaderData* dead = data; 536 dead->unload(); 537 data = data->next(); 538 // Remove from loader list. 539 // This class loader data will no longer be found 540 // in the ClassLoaderDataGraph. 541 if (prev != NULL) { 542 prev->set_next(data); 543 } else { 544 assert(dead == _head, "sanity check"); 545 _head = data; 546 } 547 dead->set_next(_unloading); 548 _unloading = dead; 549 } 550 551 log_debug(class, loader, data)("do_unloading: loaders processed %u, loaders removed %u", loaders_processed, loaders_removed); 552 553 return seen_dead_loader; 554 } 555 556 // There's at least one dead class loader. Purge refererences of healthy module 557 // reads lists and package export lists to modules belonging to dead loaders. 558 void ClassLoaderDataGraph::clean_module_and_package_info() { 559 assert_locked_or_safepoint(ClassLoaderDataGraph_lock); 560 561 ClassLoaderData* data = _head; 562 while (data != NULL) { 563 // Remove entries in the dictionary of live class loader that have 564 // initiated loading classes in a dead class loader. 565 if (data->dictionary() != NULL) { 566 data->dictionary()->do_unloading(); 567 } 568 // Walk a ModuleEntry's reads, and a PackageEntry's exports 569 // lists to determine if there are modules on those lists that are now 570 // dead and should be removed. A module's life cycle is equivalent 571 // to its defining class loader's life cycle. Since a module is 572 // considered dead if its class loader is dead, these walks must 573 // occur after each class loader's aliveness is determined. 574 if (data->packages() != NULL) { 575 data->packages()->purge_all_package_exports(); 576 } 577 if (data->modules_defined()) { 578 data->modules()->purge_all_module_reads(); 579 } 580 data = data->next(); 581 } 582 } 583 584 void ClassLoaderDataGraph::purge() { 585 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); 586 ClassLoaderData* list = _unloading; 587 _unloading = NULL; 588 ClassLoaderData* next = list; 589 bool classes_unloaded = false; 590 while (next != NULL) { 591 ClassLoaderData* purge_me = next; 592 next = purge_me->next(); 593 delete purge_me; 594 classes_unloaded = true; 595 } 596 if (classes_unloaded) { 597 Metaspace::purge(); 598 set_metaspace_oom(false); 599 } 600 } 601 602 int ClassLoaderDataGraph::resize_if_needed() { 603 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); 604 int resized = 0; 605 if (Dictionary::does_any_dictionary_needs_resizing()) { 606 FOR_ALL_DICTIONARY(cld) { 607 if (cld->dictionary()->resize_if_needed()) { 608 resized++; 609 } 610 } 611 } 612 return resized; 613 } 614 615 ClassLoaderDataGraphKlassIteratorAtomic::ClassLoaderDataGraphKlassIteratorAtomic() 616 : _next_klass(NULL) { 617 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); 618 ClassLoaderData* cld = ClassLoaderDataGraph::_head; 619 Klass* klass = NULL; 620 621 // Find the first klass in the CLDG. 622 while (cld != NULL) { 623 assert_locked_or_safepoint(cld->metaspace_lock()); 624 klass = cld->_klasses; 625 if (klass != NULL) { 626 _next_klass = klass; 627 return; 628 } 629 cld = cld->next(); 630 } 631 } 632 633 Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass_in_cldg(Klass* klass) { 634 Klass* next = klass->next_link(); 635 if (next != NULL) { 636 return next; 637 } 638 639 // No more klasses in the current CLD. Time to find a new CLD. 640 ClassLoaderData* cld = klass->class_loader_data(); 641 assert_locked_or_safepoint(cld->metaspace_lock()); 642 while (next == NULL) { 643 cld = cld->next(); 644 if (cld == NULL) { 645 break; 646 } 647 next = cld->_klasses; 648 } 649 650 return next; 651 } 652 653 Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass() { 654 Klass* head = _next_klass; 655 656 while (head != NULL) { 657 Klass* next = next_klass_in_cldg(head); 658 659 Klass* old_head = Atomic::cmpxchg(next, &_next_klass, head); 660 661 if (old_head == head) { 662 return head; // Won the CAS. 663 } 664 665 head = old_head; 666 } 667 668 // Nothing more for the iterator to hand out. 669 assert(head == NULL, "head is " PTR_FORMAT ", expected not null:", p2i(head)); 670 return NULL; 671 } 672 673 ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() { 674 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); 675 _data = ClassLoaderDataGraph::_head; 676 } 677 678 ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {} 679 680 ClassLoaderMetaspace* ClassLoaderDataGraphMetaspaceIterator::get_next() { 681 assert(_data != NULL, "Should not be NULL in call to the iterator"); 682 ClassLoaderMetaspace* result = _data->metaspace_or_null(); 683 _data = _data->next(); 684 // This result might be NULL for class loaders without metaspace 685 // yet. It would be nice to return only non-null results but 686 // there is no guarantee that there will be a non-null result 687 // down the list so the caller is going to have to check. 688 return result; 689 } 690 691 #ifndef PRODUCT 692 // callable from debugger 693 extern "C" int print_loader_data_graph() { 694 ResourceMark rm; 695 ClassLoaderDataGraph::print_on(tty); 696 return 0; 697 } 698 699 void ClassLoaderDataGraph::verify() { 700 ClassLoaderDataGraphIterator iter; 701 while (ClassLoaderData* cld = iter.get_next()) { 702 cld->verify(); 703 } 704 } 705 706 void ClassLoaderDataGraph::print_on(outputStream * const out) { 707 ClassLoaderDataGraphIterator iter; 708 while (ClassLoaderData* cld = iter.get_next()) { 709 cld->print_on(out); 710 } 711 } 712 #endif // PRODUCT