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