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