1 /* 2 * Copyright (c) 2012, 2017, 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 // A ClassLoaderData identifies the full set of class types that a class 26 // loader's name resolution strategy produces for a given configuration of the 27 // class loader. 28 // Class types in the ClassLoaderData may be defined by from class file binaries 29 // provided by the class loader, or from other class loader it interacts with 30 // according to its name resolution strategy. 31 // 32 // Class loaders that implement a deterministic name resolution strategy 33 // (including with respect to their delegation behavior), such as the boot, the 34 // platform, and the system loaders of the JDK's built-in class loader 35 // hierarchy, always produce the same linkset for a given configuration. 36 // 37 // ClassLoaderData carries information related to a linkset (e.g., 38 // metaspace holding its klass definitions). 39 // The System Dictionary and related data structures (e.g., placeholder table, 40 // loader constraints table) as well as the runtime representation of classes 41 // only reference ClassLoaderData. 42 // 43 // Instances of java.lang.ClassLoader holds a pointer to a ClassLoaderData that 44 // that represent the loader's "linking domain" in the JVM. 45 // 46 // The bootstrap loader (represented by NULL) also has a ClassLoaderData, 47 // the singleton class the_null_class_loader_data(). 48 49 #include "precompiled.hpp" 50 #include "classfile/classLoaderData.hpp" 51 #include "classfile/classLoaderData.inline.hpp" 52 #include "classfile/javaClasses.hpp" 53 #include "classfile/metadataOnStackMark.hpp" 54 #include "classfile/moduleEntry.hpp" 55 #include "classfile/packageEntry.hpp" 56 #include "classfile/systemDictionary.hpp" 57 #include "code/codeCache.hpp" 58 #include "gc/shared/gcLocker.hpp" 59 #include "logging/log.hpp" 60 #include "memory/metadataFactory.hpp" 61 #include "memory/metaspaceShared.hpp" 62 #include "memory/oopFactory.hpp" 63 #include "memory/resourceArea.hpp" 64 #include "oops/objArrayOop.inline.hpp" 65 #include "oops/oop.inline.hpp" 66 #include "runtime/atomic.hpp" 67 #include "runtime/javaCalls.hpp" 68 #include "runtime/jniHandles.hpp" 69 #include "runtime/mutex.hpp" 70 #include "runtime/orderAccess.hpp" 71 #include "runtime/safepoint.hpp" 72 #include "runtime/synchronizer.hpp" 73 #include "utilities/growableArray.hpp" 74 #include "utilities/macros.hpp" 75 #include "utilities/ostream.hpp" 76 #if INCLUDE_TRACE 77 #include "trace/tracing.hpp" 78 #endif 79 80 // helper function to avoid in-line casts 81 template <typename T> static T* load_ptr_acquire(T* volatile *p) { 82 return static_cast<T*>(OrderAccess::load_ptr_acquire(p)); 83 } 84 85 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL; 86 87 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) : 88 _class_loader(h_class_loader()), 89 _is_anonymous(is_anonymous), 90 // An anonymous class loader data doesn't have anything to keep 91 // it from being unloaded during parsing of the anonymous class. 92 // The null-class-loader should always be kept alive. 93 _keep_alive((is_anonymous || h_class_loader.is_null()) ? 1 : 0), 94 _metaspace(NULL), _unloading(false), _klasses(NULL), 95 _modules(NULL), _packages(NULL), 96 _claimed(0), _jmethod_ids(NULL), _handles(), _deallocate_list(NULL), 97 _next(NULL), _dependencies(dependencies), 98 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true, 99 Monitor::_safepoint_check_never)) { 100 101 // A ClassLoaderData created solely for an anonymous class should never have a 102 // ModuleEntryTable or PackageEntryTable created for it. The defining package 103 // and module for an anonymous class will be found in its host class. 104 if (!is_anonymous) { 105 if (h_class_loader.is_null()) { 106 // Create unnamed module for boot loader 107 _unnamed_module = ModuleEntry::create_boot_unnamed_module(this); 108 } else { 109 // Create unnamed module for all other loaders 110 _unnamed_module = ModuleEntry::create_unnamed_module(this); 111 } 112 } else { 113 _unnamed_module = NULL; 114 } 115 TRACE_INIT_ID(this); 116 } 117 118 void ClassLoaderData::init_dependencies(TRAPS) { 119 assert(!Universe::is_fully_initialized(), "should only be called when initializing"); 120 assert(is_the_null_class_loader_data(), "should only call this for the null class loader"); 121 _dependencies.init(CHECK); 122 } 123 124 void ClassLoaderData::Dependencies::init(TRAPS) { 125 // Create empty dependencies array to add to. CMS requires this to be 126 // an oop so that it can track additions via card marks. We think. 127 _list_head = oopFactory::new_objectArray(2, CHECK); 128 } 129 130 ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() { 131 Chunk* c = _head; 132 while (c != NULL) { 133 Chunk* next = c->_next; 134 delete c; 135 c = next; 136 } 137 } 138 139 oop* ClassLoaderData::ChunkedHandleList::add(oop o) { 140 if (_head == NULL || _head->_size == Chunk::CAPACITY) { 141 Chunk* next = new Chunk(_head); 142 OrderAccess::release_store_ptr(&_head, next); 143 } 144 oop* handle = &_head->_data[_head->_size]; 145 *handle = o; 146 OrderAccess::release_store(&_head->_size, _head->_size + 1); 147 return handle; 148 } 149 150 inline void ClassLoaderData::ChunkedHandleList::oops_do_chunk(OopClosure* f, Chunk* c, const juint size) { 151 for (juint i = 0; i < size; i++) { 152 if (c->_data[i] != NULL) { 153 f->do_oop(&c->_data[i]); 154 } 155 } 156 } 157 158 void ClassLoaderData::ChunkedHandleList::oops_do(OopClosure* f) { 159 Chunk* head = (Chunk*) OrderAccess::load_ptr_acquire(&_head); 160 if (head != NULL) { 161 // Must be careful when reading size of head 162 oops_do_chunk(f, head, OrderAccess::load_acquire(&head->_size)); 163 for (Chunk* c = head->_next; c != NULL; c = c->_next) { 164 oops_do_chunk(f, c, c->_size); 165 } 166 } 167 } 168 169 #ifdef ASSERT 170 class VerifyContainsOopClosure : public OopClosure { 171 oop* _target; 172 bool _found; 173 174 public: 175 VerifyContainsOopClosure(oop* target) : _target(target), _found(false) {} 176 177 void do_oop(oop* p) { 178 if (p == _target) { 179 _found = true; 180 } 181 } 182 183 void do_oop(narrowOop* p) { 184 // The ChunkedHandleList should not contain any narrowOop 185 ShouldNotReachHere(); 186 } 187 188 bool found() const { 189 return _found; 190 } 191 }; 192 193 bool ClassLoaderData::ChunkedHandleList::contains(oop* p) { 194 VerifyContainsOopClosure cl(p); 195 oops_do(&cl); 196 return cl.found(); 197 } 198 #endif 199 200 bool ClassLoaderData::claim() { 201 if (_claimed == 1) { 202 return false; 203 } 204 205 return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0; 206 } 207 208 // Anonymous classes have their own ClassLoaderData that is marked to keep alive 209 // while the class is being parsed, and if the class appears on the module fixup list. 210 // Due to the uniqueness that no other class shares the anonymous class' name or 211 // ClassLoaderData, no other non-GC thread has knowledge of the anonymous class while 212 // it is being defined, therefore _keep_alive is not volatile or atomic. 213 void ClassLoaderData::inc_keep_alive() { 214 if (is_anonymous()) { 215 assert(_keep_alive >= 0, "Invalid keep alive increment count"); 216 _keep_alive++; 217 } 218 } 219 220 void ClassLoaderData::dec_keep_alive() { 221 if (is_anonymous()) { 222 assert(_keep_alive > 0, "Invalid keep alive decrement count"); 223 _keep_alive--; 224 } 225 } 226 227 void ClassLoaderData::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) { 228 if (must_claim && !claim()) { 229 return; 230 } 231 232 f->do_oop(&_class_loader); 233 _dependencies.oops_do(f); 234 235 _handles.oops_do(f); 236 237 if (klass_closure != NULL) { 238 classes_do(klass_closure); 239 } 240 } 241 242 void ClassLoaderData::Dependencies::oops_do(OopClosure* f) { 243 f->do_oop((oop*)&_list_head); 244 } 245 246 void ClassLoaderData::classes_do(KlassClosure* klass_closure) { 247 // Lock-free access requires load_ptr_acquire 248 for (Klass* k = load_ptr_acquire(&_klasses); k != NULL; k = k->next_link()) { 249 klass_closure->do_klass(k); 250 assert(k != k->next_link(), "no loops!"); 251 } 252 } 253 254 void ClassLoaderData::classes_do(void f(Klass * const)) { 255 // Lock-free access requires load_ptr_acquire 256 for (Klass* k = load_ptr_acquire(&_klasses); k != NULL; k = k->next_link()) { 257 f(k); 258 assert(k != k->next_link(), "no loops!"); 259 } 260 } 261 262 void ClassLoaderData::methods_do(void f(Method*)) { 263 // Lock-free access requires load_ptr_acquire 264 for (Klass* k = load_ptr_acquire(&_klasses); k != NULL; k = k->next_link()) { 265 if (k->is_instance_klass() && InstanceKlass::cast(k)->is_loaded()) { 266 InstanceKlass::cast(k)->methods_do(f); 267 } 268 } 269 } 270 271 void ClassLoaderData::loaded_classes_do(KlassClosure* klass_closure) { 272 // Lock to avoid classes being modified/added/removed during iteration 273 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); 274 for (Klass* k = _klasses; k != NULL; k = k->next_link()) { 275 // Do not filter ArrayKlass oops here... 276 if (k->is_array_klass() || (k->is_instance_klass() && InstanceKlass::cast(k)->is_loaded())) { 277 klass_closure->do_klass(k); 278 } 279 } 280 } 281 282 void ClassLoaderData::classes_do(void f(InstanceKlass*)) { 283 // Lock-free access requires load_ptr_acquire 284 for (Klass* k = load_ptr_acquire(&_klasses); k != NULL; k = k->next_link()) { 285 if (k->is_instance_klass()) { 286 f(InstanceKlass::cast(k)); 287 } 288 assert(k != k->next_link(), "no loops!"); 289 } 290 } 291 292 void ClassLoaderData::modules_do(void f(ModuleEntry*)) { 293 assert_locked_or_safepoint(Module_lock); 294 if (_unnamed_module != NULL) { 295 f(_unnamed_module); 296 } 297 if (_modules != NULL) { 298 for (int i = 0; i < _modules->table_size(); i++) { 299 for (ModuleEntry* entry = _modules->bucket(i); 300 entry != NULL; 301 entry = entry->next()) { 302 f(entry); 303 } 304 } 305 } 306 } 307 308 void ClassLoaderData::packages_do(void f(PackageEntry*)) { 309 // Lock-free access requires load_ptr_acquire 310 PackageEntryTable* packages = load_ptr_acquire(&_packages); 311 if (packages != NULL) { 312 for (int i = 0; i < packages->table_size(); i++) { 313 for (PackageEntry* entry = packages->bucket(i); 314 entry != NULL; 315 entry = entry->next()) { 316 f(entry); 317 } 318 } 319 } 320 } 321 322 void ClassLoaderData::record_dependency(const Klass* k, TRAPS) { 323 assert(k != NULL, "invariant"); 324 325 ClassLoaderData * const from_cld = this; 326 ClassLoaderData * const to_cld = k->class_loader_data(); 327 328 // Dependency to the null class loader data doesn't need to be recorded 329 // because the null class loader data never goes away. 330 if (to_cld->is_the_null_class_loader_data()) { 331 return; 332 } 333 334 oop to; 335 if (to_cld->is_anonymous()) { 336 // Anonymous class dependencies are through the mirror. 337 to = k->java_mirror(); 338 } else { 339 to = to_cld->class_loader(); 340 341 // If from_cld is anonymous, even if it's class_loader is a parent of 'to' 342 // we still have to add it. The class_loader won't keep from_cld alive. 343 if (!from_cld->is_anonymous()) { 344 // Check that this dependency isn't from the same or parent class_loader 345 oop from = from_cld->class_loader(); 346 347 oop curr = from; 348 while (curr != NULL) { 349 if (curr == to) { 350 return; // this class loader is in the parent list, no need to add it. 351 } 352 curr = java_lang_ClassLoader::parent(curr); 353 } 354 } 355 } 356 357 // It's a dependency we won't find through GC, add it. This is relatively rare 358 // Must handle over GC point. 359 Handle dependency(THREAD, to); 360 from_cld->_dependencies.add(dependency, CHECK); 361 } 362 363 364 void ClassLoaderData::Dependencies::add(Handle dependency, TRAPS) { 365 // Check first if this dependency is already in the list. 366 // Save a pointer to the last to add to under the lock. 367 objArrayOop ok = _list_head; 368 objArrayOop last = NULL; 369 while (ok != NULL) { 370 last = ok; 371 if (ok->obj_at(0) == dependency()) { 372 // Don't need to add it 373 return; 374 } 375 ok = (objArrayOop)ok->obj_at(1); 376 } 377 378 // Must handle over GC points 379 assert (last != NULL, "dependencies should be initialized"); 380 objArrayHandle last_handle(THREAD, last); 381 382 // Create a new dependency node with fields for (class_loader or mirror, next) 383 objArrayOop deps = oopFactory::new_objectArray(2, CHECK); 384 deps->obj_at_put(0, dependency()); 385 386 // Must handle over GC points 387 objArrayHandle new_dependency(THREAD, deps); 388 389 // Add the dependency under lock 390 locked_add(last_handle, new_dependency, THREAD); 391 } 392 393 void ClassLoaderData::Dependencies::locked_add(objArrayHandle last_handle, 394 objArrayHandle new_dependency, 395 Thread* THREAD) { 396 397 // Have to lock and put the new dependency on the end of the dependency 398 // array so the card mark for CMS sees that this dependency is new. 399 // Can probably do this lock free with some effort. 400 ObjectLocker ol(Handle(THREAD, _list_head), THREAD); 401 402 oop loader_or_mirror = new_dependency->obj_at(0); 403 404 // Since the dependencies are only added, add to the end. 405 objArrayOop end = last_handle(); 406 objArrayOop last = NULL; 407 while (end != NULL) { 408 last = end; 409 // check again if another thread added it to the end. 410 if (end->obj_at(0) == loader_or_mirror) { 411 // Don't need to add it 412 return; 413 } 414 end = (objArrayOop)end->obj_at(1); 415 } 416 assert (last != NULL, "dependencies should be initialized"); 417 // fill in the first element with the oop in new_dependency. 418 if (last->obj_at(0) == NULL) { 419 last->obj_at_put(0, new_dependency->obj_at(0)); 420 } else { 421 last->obj_at_put(1, new_dependency()); 422 } 423 } 424 425 void ClassLoaderDataGraph::clear_claimed_marks() { 426 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { 427 cld->clear_claimed(); 428 } 429 } 430 431 void ClassLoaderData::add_class(Klass* k, bool publicize /* true */) { 432 { 433 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); 434 Klass* old_value = _klasses; 435 k->set_next_link(old_value); 436 // Link the new item into the list, making sure the linked class is stable 437 // since the list can be walked without a lock 438 OrderAccess::release_store_ptr(&_klasses, k); 439 } 440 441 if (publicize && k->class_loader_data() != NULL) { 442 ResourceMark rm; 443 log_trace(class, loader, data)("Adding k: " PTR_FORMAT " %s to CLD: " 444 PTR_FORMAT " loader: " PTR_FORMAT " %s", 445 p2i(k), 446 k->external_name(), 447 p2i(k->class_loader_data()), 448 p2i((void *)k->class_loader()), 449 loader_name()); 450 } 451 } 452 453 // Remove a klass from the _klasses list for scratch_class during redefinition 454 // or parsed class in the case of an error. 455 void ClassLoaderData::remove_class(Klass* scratch_class) { 456 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint"); 457 Klass* prev = NULL; 458 for (Klass* k = _klasses; k != NULL; k = k->next_link()) { 459 if (k == scratch_class) { 460 if (prev == NULL) { 461 _klasses = k->next_link(); 462 } else { 463 Klass* next = k->next_link(); 464 prev->set_next_link(next); 465 } 466 return; 467 } 468 prev = k; 469 assert(k != k->next_link(), "no loops!"); 470 } 471 ShouldNotReachHere(); // should have found this class!! 472 } 473 474 void ClassLoaderData::unload() { 475 _unloading = true; 476 477 // Tell serviceability tools these classes are unloading 478 classes_do(InstanceKlass::notify_unload_class); 479 480 if (log_is_enabled(Debug, class, loader, data)) { 481 ResourceMark rm; 482 outputStream* log = Log(class, loader, data)::debug_stream(); 483 log->print(": unload loader data " INTPTR_FORMAT, p2i(this)); 484 log->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)class_loader()), 485 loader_name()); 486 if (is_anonymous()) { 487 log->print(" for anonymous class " INTPTR_FORMAT " ", p2i(_klasses)); 488 } 489 log->cr(); 490 } 491 492 // In some rare cases items added to this list will not be freed elsewhere. 493 // To keep it simple, just free everything in it here. 494 free_deallocate_list(); 495 } 496 497 PackageEntryTable* ClassLoaderData::packages() { 498 // Lazily create the package entry table at first request. 499 // Lock-free access requires load_ptr_acquire. 500 PackageEntryTable* packages = load_ptr_acquire(&_packages); 501 if (packages == NULL) { 502 MutexLockerEx m1(metaspace_lock(), Mutex::_no_safepoint_check_flag); 503 // Check if _packages got allocated while we were waiting for this lock. 504 if ((packages = _packages) == NULL) { 505 packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size); 506 // Ensure _packages is stable, since it is examined without a lock 507 OrderAccess::release_store_ptr(&_packages, packages); 508 } 509 } 510 return packages; 511 } 512 513 ModuleEntryTable* ClassLoaderData::modules() { 514 // Lazily create the module entry table at first request. 515 // Lock-free access requires load_ptr_acquire. 516 ModuleEntryTable* modules = load_ptr_acquire(&_modules); 517 if (modules == NULL) { 518 MutexLocker m1(Module_lock); 519 // Check if _modules got allocated while we were waiting for this lock. 520 if ((modules = _modules) == NULL) { 521 modules = new ModuleEntryTable(ModuleEntryTable::_moduletable_entry_size); 522 523 { 524 MutexLockerEx m1(metaspace_lock(), Mutex::_no_safepoint_check_flag); 525 // Ensure _modules is stable, since it is examined without a lock 526 OrderAccess::release_store_ptr(&_modules, modules); 527 } 528 } 529 } 530 return modules; 531 } 532 533 oop ClassLoaderData::keep_alive_object() const { 534 assert_locked_or_safepoint(_metaspace_lock); 535 assert(!keep_alive(), "Don't use with CLDs that are artificially kept alive"); 536 return is_anonymous() ? _klasses->java_mirror() : class_loader(); 537 } 538 539 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const { 540 bool alive = keep_alive() // null class loader and incomplete anonymous klasses. 541 || is_alive_closure->do_object_b(keep_alive_object()); 542 543 return alive; 544 } 545 546 ClassLoaderData::~ClassLoaderData() { 547 // Release C heap structures for all the classes. 548 classes_do(InstanceKlass::release_C_heap_structures); 549 550 // Release C heap allocated hashtable for all the packages. 551 if (_packages != NULL) { 552 // Destroy the table itself 553 delete _packages; 554 _packages = NULL; 555 } 556 557 // Release C heap allocated hashtable for all the modules. 558 if (_modules != NULL) { 559 // Destroy the table itself 560 delete _modules; 561 _modules = NULL; 562 } 563 564 if (_unnamed_module != NULL) { 565 FREE_C_HEAP_ARRAY(char, _unnamed_module); 566 _unnamed_module = NULL; 567 } 568 569 // release the metaspace 570 Metaspace *m = _metaspace; 571 if (m != NULL) { 572 _metaspace = NULL; 573 delete m; 574 } 575 // Clear all the JNI handles for methods 576 // These aren't deallocated and are going to look like a leak, but that's 577 // needed because we can't really get rid of jmethodIDs because we don't 578 // know when native code is going to stop using them. The spec says that 579 // they're "invalid" but existing programs likely rely on their being 580 // NULL after class unloading. 581 if (_jmethod_ids != NULL) { 582 Method::clear_jmethod_ids(this); 583 } 584 // Delete lock 585 delete _metaspace_lock; 586 587 // Delete free list 588 if (_deallocate_list != NULL) { 589 delete _deallocate_list; 590 } 591 } 592 593 // Returns true if this class loader data is for the system class loader. 594 bool ClassLoaderData::is_system_class_loader_data() const { 595 return SystemDictionary::is_system_class_loader(class_loader()); 596 } 597 598 // Returns true if this class loader data is for the platform class loader. 599 bool ClassLoaderData::is_platform_class_loader_data() const { 600 return SystemDictionary::is_platform_class_loader(class_loader()); 601 } 602 603 // Returns true if this class loader data is one of the 3 builtin 604 // (boot, application/system or platform) class loaders. Note, the 605 // builtin loaders are not freed by a GC. 606 bool ClassLoaderData::is_builtin_class_loader_data() const { 607 return (is_the_null_class_loader_data() || 608 SystemDictionary::is_system_class_loader(class_loader()) || 609 SystemDictionary::is_platform_class_loader(class_loader())); 610 } 611 612 Metaspace* ClassLoaderData::metaspace_non_null() { 613 assert(!DumpSharedSpaces, "wrong metaspace!"); 614 // If the metaspace has not been allocated, create a new one. Might want 615 // to create smaller arena for Reflection class loaders also. 616 // The reason for the delayed allocation is because some class loaders are 617 // simply for delegating with no metadata of their own. 618 // Lock-free access requires load_ptr_acquire. 619 Metaspace* metaspace = load_ptr_acquire(&_metaspace); 620 if (metaspace == NULL) { 621 MutexLockerEx ml(_metaspace_lock, Mutex::_no_safepoint_check_flag); 622 // Check if _metaspace got allocated while we were waiting for this lock. 623 if ((metaspace = _metaspace) == NULL) { 624 if (this == the_null_class_loader_data()) { 625 assert (class_loader() == NULL, "Must be"); 626 metaspace = new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType); 627 } else if (is_anonymous()) { 628 if (class_loader() != NULL) { 629 log_trace(class, loader, data)("is_anonymous: %s", class_loader()->klass()->internal_name()); 630 } 631 metaspace = new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType); 632 } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) { 633 if (class_loader() != NULL) { 634 log_trace(class, loader, data)("is_reflection: %s", class_loader()->klass()->internal_name()); 635 } 636 metaspace = new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType); 637 } else { 638 metaspace = new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType); 639 } 640 // Ensure _metaspace is stable, since it is examined without a lock 641 OrderAccess::release_store_ptr(&_metaspace, metaspace); 642 } 643 } 644 return metaspace; 645 } 646 647 jobject ClassLoaderData::add_handle(Handle h) { 648 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); 649 return (jobject) _handles.add(h()); 650 } 651 652 void ClassLoaderData::remove_handle_unsafe(jobject h) { 653 assert(_handles.contains((oop*) h), "Got unexpected handle " PTR_FORMAT, p2i((oop*) h)); 654 *((oop*) h) = NULL; 655 } 656 657 // Add this metadata pointer to be freed when it's safe. This is only during 658 // class unloading because Handles might point to this metadata field. 659 void ClassLoaderData::add_to_deallocate_list(Metadata* m) { 660 // Metadata in shared region isn't deleted. 661 if (!m->is_shared()) { 662 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); 663 if (_deallocate_list == NULL) { 664 _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true); 665 } 666 _deallocate_list->append_if_missing(m); 667 } 668 } 669 670 // Deallocate free metadata on the free list. How useful the PermGen was! 671 void ClassLoaderData::free_deallocate_list() { 672 // Don't need lock, at safepoint 673 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint"); 674 if (_deallocate_list == NULL) { 675 return; 676 } 677 // Go backwards because this removes entries that are freed. 678 for (int i = _deallocate_list->length() - 1; i >= 0; i--) { 679 Metadata* m = _deallocate_list->at(i); 680 if (!m->on_stack()) { 681 _deallocate_list->remove_at(i); 682 // There are only three types of metadata that we deallocate directly. 683 // Cast them so they can be used by the template function. 684 if (m->is_method()) { 685 MetadataFactory::free_metadata(this, (Method*)m); 686 } else if (m->is_constantPool()) { 687 MetadataFactory::free_metadata(this, (ConstantPool*)m); 688 } else if (m->is_klass()) { 689 MetadataFactory::free_metadata(this, (InstanceKlass*)m); 690 } else { 691 ShouldNotReachHere(); 692 } 693 } else { 694 // Metadata is alive. 695 // If scratch_class is on stack then it shouldn't be on this list! 696 assert(!m->is_klass() || !((InstanceKlass*)m)->is_scratch_class(), 697 "scratch classes on this list should be dead"); 698 // Also should assert that other metadata on the list was found in handles. 699 } 700 } 701 } 702 703 // These anonymous class loaders are to contain classes used for JSR292 704 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(oop loader, TRAPS) { 705 // Add a new class loader data to the graph. 706 Handle lh(THREAD, loader); 707 return ClassLoaderDataGraph::add(lh, true, THREAD); 708 } 709 710 const char* ClassLoaderData::loader_name() { 711 // Handles null class loader 712 return SystemDictionary::loader_name(class_loader()); 713 } 714 715 #ifndef PRODUCT 716 // Define to dump klasses 717 #undef CLD_DUMP_KLASSES 718 719 void ClassLoaderData::dump(outputStream * const out) { 720 out->print("ClassLoaderData CLD: " PTR_FORMAT ", loader: " PTR_FORMAT ", loader_klass: " PTR_FORMAT " %s {", 721 p2i(this), p2i((void *)class_loader()), 722 p2i(class_loader() != NULL ? class_loader()->klass() : NULL), loader_name()); 723 if (claimed()) out->print(" claimed "); 724 if (is_unloading()) out->print(" unloading "); 725 out->cr(); 726 if (metaspace_or_null() != NULL) { 727 out->print_cr("metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null())); 728 metaspace_or_null()->dump(out); 729 } else { 730 out->print_cr("metaspace: NULL"); 731 } 732 733 #ifdef CLD_DUMP_KLASSES 734 if (Verbose) { 735 Klass* k = _klasses; 736 while (k != NULL) { 737 out->print_cr("klass " PTR_FORMAT ", %s, CT: %d, MUT: %d", k, k->name()->as_C_string(), 738 k->has_modified_oops(), k->has_accumulated_modified_oops()); 739 assert(k != k->next_link(), "no loops!"); 740 k = k->next_link(); 741 } 742 } 743 #endif // CLD_DUMP_KLASSES 744 #undef CLD_DUMP_KLASSES 745 if (_jmethod_ids != NULL) { 746 Method::print_jmethod_ids(this, out); 747 } 748 out->print_cr("}"); 749 } 750 #endif // PRODUCT 751 752 void ClassLoaderData::verify() { 753 assert_locked_or_safepoint(_metaspace_lock); 754 oop cl = class_loader(); 755 756 guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same"); 757 guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be"); 758 759 // Verify the integrity of the allocated space. 760 if (metaspace_or_null() != NULL) { 761 metaspace_or_null()->verify(); 762 } 763 764 for (Klass* k = _klasses; k != NULL; k = k->next_link()) { 765 guarantee(k->class_loader_data() == this, "Must be the same"); 766 k->verify(); 767 assert(k != k->next_link(), "no loops!"); 768 } 769 } 770 771 bool ClassLoaderData::contains_klass(Klass* klass) { 772 // Lock-free access requires load_ptr_acquire 773 for (Klass* k = load_ptr_acquire(&_klasses); k != NULL; k = k->next_link()) { 774 if (k == klass) return true; 775 } 776 return false; 777 } 778 779 780 // GC root of class loader data created. 781 ClassLoaderData* ClassLoaderDataGraph::_head = NULL; 782 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL; 783 ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL; 784 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL; 785 786 bool ClassLoaderDataGraph::_should_purge = false; 787 bool ClassLoaderDataGraph::_metaspace_oom = false; 788 789 // Add a new class loader data node to the list. Assign the newly created 790 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field 791 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous, TRAPS) { 792 // We need to allocate all the oops for the ClassLoaderData before allocating the 793 // actual ClassLoaderData object. 794 ClassLoaderData::Dependencies dependencies(CHECK_NULL); 795 796 NoSafepointVerifier no_safepoints; // we mustn't GC until we've installed the 797 // ClassLoaderData in the graph since the CLD 798 // contains unhandled oops 799 800 ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous, dependencies); 801 802 803 if (!is_anonymous) { 804 ClassLoaderData** cld_addr = java_lang_ClassLoader::loader_data_addr(loader()); 805 // First, Atomically set it 806 ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL); 807 if (old != NULL) { 808 delete cld; 809 // Returns the data. 810 return old; 811 } 812 } 813 814 // We won the race, and therefore the task of adding the data to the list of 815 // class loader data 816 ClassLoaderData** list_head = &_head; 817 ClassLoaderData* next = _head; 818 819 do { 820 cld->set_next(next); 821 ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next); 822 if (exchanged == next) { 823 if (log_is_enabled(Debug, class, loader, data)) { 824 PauseNoSafepointVerifier pnsv(&no_safepoints); // Need safe points for JavaCalls::call_virtual 825 log_creation(loader, cld, CHECK_NULL); 826 } 827 return cld; 828 } 829 next = exchanged; 830 } while (true); 831 } 832 833 void ClassLoaderDataGraph::log_creation(Handle loader, ClassLoaderData* cld, TRAPS) { 834 Handle string; 835 if (loader.not_null()) { 836 // Include the result of loader.toString() in the output. This allows 837 // the user of the log to identify the class loader instance. 838 JavaValue result(T_OBJECT); 839 Klass* spec_klass = SystemDictionary::ClassLoader_klass(); 840 JavaCalls::call_virtual(&result, 841 loader, 842 spec_klass, 843 vmSymbols::toString_name(), 844 vmSymbols::void_string_signature(), 845 CHECK); 846 assert(result.get_type() == T_OBJECT, "just checking"); 847 string = Handle(THREAD, (oop)result.get_jobject()); 848 } 849 850 ResourceMark rm; 851 outputStream* log = Log(class, loader, data)::debug_stream(); 852 log->print("create class loader data " INTPTR_FORMAT, p2i(cld)); 853 log->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)cld->class_loader()), 854 cld->loader_name()); 855 856 if (string.not_null()) { 857 log->print(": "); 858 java_lang_String::print(string(), log); 859 } 860 log->cr(); 861 } 862 863 864 void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) { 865 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { 866 cld->oops_do(f, klass_closure, must_claim); 867 } 868 } 869 870 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) { 871 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { 872 if (cld->keep_alive()) { 873 cld->oops_do(f, klass_closure, must_claim); 874 } 875 } 876 } 877 878 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) { 879 if (ClassUnloading) { 880 keep_alive_oops_do(f, klass_closure, must_claim); 881 } else { 882 oops_do(f, klass_closure, must_claim); 883 } 884 } 885 886 void ClassLoaderDataGraph::cld_do(CLDClosure* cl) { 887 for (ClassLoaderData* cld = _head; cl != NULL && cld != NULL; cld = cld->next()) { 888 cl->do_cld(cld); 889 } 890 } 891 892 void ClassLoaderDataGraph::cld_unloading_do(CLDClosure* cl) { 893 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); 894 // Only walk the head until any clds not purged from prior unloading 895 // (CMS doesn't purge right away). 896 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) { 897 assert(cld->is_unloading(), "invariant"); 898 cl->do_cld(cld); 899 } 900 } 901 902 void ClassLoaderDataGraph::roots_cld_do(CLDClosure* strong, CLDClosure* weak) { 903 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->_next) { 904 CLDClosure* closure = cld->keep_alive() ? strong : weak; 905 if (closure != NULL) { 906 closure->do_cld(cld); 907 } 908 } 909 } 910 911 void ClassLoaderDataGraph::keep_alive_cld_do(CLDClosure* cl) { 912 roots_cld_do(cl, NULL); 913 } 914 915 void ClassLoaderDataGraph::always_strong_cld_do(CLDClosure* cl) { 916 if (ClassUnloading) { 917 keep_alive_cld_do(cl); 918 } else { 919 cld_do(cl); 920 } 921 } 922 923 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) { 924 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { 925 cld->classes_do(klass_closure); 926 } 927 } 928 929 void ClassLoaderDataGraph::classes_do(void f(Klass* const)) { 930 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { 931 cld->classes_do(f); 932 } 933 } 934 935 void ClassLoaderDataGraph::methods_do(void f(Method*)) { 936 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { 937 cld->methods_do(f); 938 } 939 } 940 941 void ClassLoaderDataGraph::modules_do(void f(ModuleEntry*)) { 942 assert_locked_or_safepoint(Module_lock); 943 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { 944 cld->modules_do(f); 945 } 946 } 947 948 void ClassLoaderDataGraph::modules_unloading_do(void f(ModuleEntry*)) { 949 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); 950 // Only walk the head until any clds not purged from prior unloading 951 // (CMS doesn't purge right away). 952 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) { 953 assert(cld->is_unloading(), "invariant"); 954 cld->modules_do(f); 955 } 956 } 957 958 void ClassLoaderDataGraph::packages_do(void f(PackageEntry*)) { 959 assert_locked_or_safepoint(Module_lock); 960 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { 961 cld->packages_do(f); 962 } 963 } 964 965 void ClassLoaderDataGraph::packages_unloading_do(void f(PackageEntry*)) { 966 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); 967 // Only walk the head until any clds not purged from prior unloading 968 // (CMS doesn't purge right away). 969 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) { 970 assert(cld->is_unloading(), "invariant"); 971 cld->packages_do(f); 972 } 973 } 974 975 void ClassLoaderDataGraph::loaded_classes_do(KlassClosure* klass_closure) { 976 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { 977 cld->loaded_classes_do(klass_closure); 978 } 979 } 980 981 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) { 982 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); 983 // Only walk the head until any clds not purged from prior unloading 984 // (CMS doesn't purge right away). 985 for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) { 986 assert(cld->is_unloading(), "invariant"); 987 cld->classes_do(f); 988 } 989 } 990 991 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() { 992 assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?"); 993 994 GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>(); 995 996 // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true); 997 ClassLoaderData* curr = _head; 998 while (curr != _saved_head) { 999 if (!curr->claimed()) { 1000 array->push(curr); 1001 1002 if (log_is_enabled(Debug, class, loader, data)) { 1003 outputStream* log = Log(class, loader, data)::debug_stream(); 1004 log->print("found new CLD: "); 1005 curr->print_value_on(log); 1006 log->cr(); 1007 } 1008 } 1009 1010 curr = curr->_next; 1011 } 1012 1013 return array; 1014 } 1015 1016 bool ClassLoaderDataGraph::unload_list_contains(const void* x) { 1017 assert(SafepointSynchronize::is_at_safepoint(), "only safe to call at safepoint"); 1018 for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) { 1019 if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) { 1020 return true; 1021 } 1022 } 1023 return false; 1024 } 1025 1026 #ifndef PRODUCT 1027 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) { 1028 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) { 1029 if (loader_data == data) { 1030 return true; 1031 } 1032 } 1033 1034 return false; 1035 } 1036 #endif // PRODUCT 1037 1038 1039 // Move class loader data from main list to the unloaded list for unloading 1040 // and deallocation later. 1041 bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive_closure, 1042 bool clean_previous_versions) { 1043 1044 ClassLoaderData* data = _head; 1045 ClassLoaderData* prev = NULL; 1046 bool seen_dead_loader = false; 1047 1048 // Mark metadata seen on the stack only so we can delete unneeded entries. 1049 // Only walk all metadata, including the expensive code cache walk, for Full GC 1050 // and only if class redefinition and if there's previous versions of 1051 // Klasses to delete. 1052 bool walk_all_metadata = clean_previous_versions && 1053 JvmtiExport::has_redefined_a_class() && 1054 InstanceKlass::has_previous_versions_and_reset(); 1055 MetadataOnStackMark md_on_stack(walk_all_metadata); 1056 1057 // Save previous _unloading pointer for CMS which may add to unloading list before 1058 // purging and we don't want to rewalk the previously unloaded class loader data. 1059 _saved_unloading = _unloading; 1060 1061 data = _head; 1062 while (data != NULL) { 1063 if (data->is_alive(is_alive_closure)) { 1064 // clean metaspace 1065 if (walk_all_metadata) { 1066 data->classes_do(InstanceKlass::purge_previous_versions); 1067 } 1068 data->free_deallocate_list(); 1069 prev = data; 1070 data = data->next(); 1071 continue; 1072 } 1073 seen_dead_loader = true; 1074 ClassLoaderData* dead = data; 1075 dead->unload(); 1076 data = data->next(); 1077 // Remove from loader list. 1078 // This class loader data will no longer be found 1079 // in the ClassLoaderDataGraph. 1080 if (prev != NULL) { 1081 prev->set_next(data); 1082 } else { 1083 assert(dead == _head, "sanity check"); 1084 _head = data; 1085 } 1086 dead->set_next(_unloading); 1087 _unloading = dead; 1088 } 1089 1090 if (seen_dead_loader) { 1091 // Walk a ModuleEntry's reads and a PackageEntry's exports lists 1092 // to determine if there are modules on those lists that are now 1093 // dead and should be removed. A module's life cycle is equivalent 1094 // to its defining class loader's life cycle. Since a module is 1095 // considered dead if its class loader is dead, these walks must 1096 // occur after each class loader's aliveness is determined. 1097 data = _head; 1098 while (data != NULL) { 1099 if (data->packages_defined()) { 1100 data->packages()->purge_all_package_exports(); 1101 } 1102 if (data->modules_defined()) { 1103 data->modules()->purge_all_module_reads(); 1104 } 1105 data = data->next(); 1106 } 1107 1108 post_class_unload_events(); 1109 } 1110 1111 return seen_dead_loader; 1112 } 1113 1114 void ClassLoaderDataGraph::purge() { 1115 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); 1116 ClassLoaderData* list = _unloading; 1117 _unloading = NULL; 1118 ClassLoaderData* next = list; 1119 bool classes_unloaded = false; 1120 while (next != NULL) { 1121 ClassLoaderData* purge_me = next; 1122 next = purge_me->next(); 1123 delete purge_me; 1124 classes_unloaded = true; 1125 } 1126 if (classes_unloaded) { 1127 Metaspace::purge(); 1128 set_metaspace_oom(false); 1129 } 1130 } 1131 1132 void ClassLoaderDataGraph::post_class_unload_events() { 1133 #if INCLUDE_TRACE 1134 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); 1135 if (Tracing::enabled()) { 1136 if (Tracing::is_event_enabled(TraceClassUnloadEvent)) { 1137 assert(_unloading != NULL, "need class loader data unload list!"); 1138 _class_unload_time = Ticks::now(); 1139 classes_unloading_do(&class_unload_event); 1140 } 1141 Tracing::on_unloading_classes(); 1142 } 1143 #endif 1144 } 1145 1146 // CDS support 1147 1148 // Global metaspaces for writing information to the shared archive. When 1149 // application CDS is supported, we may need one per metaspace, so this 1150 // sort of looks like it. 1151 Metaspace* ClassLoaderData::_ro_metaspace = NULL; 1152 Metaspace* ClassLoaderData::_rw_metaspace = NULL; 1153 static bool _shared_metaspaces_initialized = false; 1154 1155 // Initialize shared metaspaces (change to call from somewhere not lazily) 1156 void ClassLoaderData::initialize_shared_metaspaces() { 1157 assert(DumpSharedSpaces, "only use this for dumping shared spaces"); 1158 assert(this == ClassLoaderData::the_null_class_loader_data(), 1159 "only supported for null loader data for now"); 1160 assert (!_shared_metaspaces_initialized, "only initialize once"); 1161 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); 1162 _ro_metaspace = new Metaspace(_metaspace_lock, Metaspace::ROMetaspaceType); 1163 _rw_metaspace = new Metaspace(_metaspace_lock, Metaspace::ReadWriteMetaspaceType); 1164 _shared_metaspaces_initialized = true; 1165 } 1166 1167 Metaspace* ClassLoaderData::ro_metaspace() { 1168 assert(_ro_metaspace != NULL, "should already be initialized"); 1169 return _ro_metaspace; 1170 } 1171 1172 Metaspace* ClassLoaderData::rw_metaspace() { 1173 assert(_rw_metaspace != NULL, "should already be initialized"); 1174 return _rw_metaspace; 1175 } 1176 1177 ClassLoaderDataGraphKlassIteratorAtomic::ClassLoaderDataGraphKlassIteratorAtomic() 1178 : _next_klass(NULL) { 1179 ClassLoaderData* cld = ClassLoaderDataGraph::_head; 1180 Klass* klass = NULL; 1181 1182 // Find the first klass in the CLDG. 1183 while (cld != NULL) { 1184 assert_locked_or_safepoint(cld->metaspace_lock()); 1185 klass = cld->_klasses; 1186 if (klass != NULL) { 1187 _next_klass = klass; 1188 return; 1189 } 1190 cld = cld->next(); 1191 } 1192 } 1193 1194 Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass_in_cldg(Klass* klass) { 1195 Klass* next = klass->next_link(); 1196 if (next != NULL) { 1197 return next; 1198 } 1199 1200 // No more klasses in the current CLD. Time to find a new CLD. 1201 ClassLoaderData* cld = klass->class_loader_data(); 1202 assert_locked_or_safepoint(cld->metaspace_lock()); 1203 while (next == NULL) { 1204 cld = cld->next(); 1205 if (cld == NULL) { 1206 break; 1207 } 1208 next = cld->_klasses; 1209 } 1210 1211 return next; 1212 } 1213 1214 Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass() { 1215 Klass* head = _next_klass; 1216 1217 while (head != NULL) { 1218 Klass* next = next_klass_in_cldg(head); 1219 1220 Klass* old_head = (Klass*)Atomic::cmpxchg_ptr(next, &_next_klass, head); 1221 1222 if (old_head == head) { 1223 return head; // Won the CAS. 1224 } 1225 1226 head = old_head; 1227 } 1228 1229 // Nothing more for the iterator to hand out. 1230 assert(head == NULL, "head is " PTR_FORMAT ", expected not null:", p2i(head)); 1231 return NULL; 1232 } 1233 1234 ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() { 1235 _data = ClassLoaderDataGraph::_head; 1236 } 1237 1238 ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {} 1239 1240 #ifndef PRODUCT 1241 // callable from debugger 1242 extern "C" int print_loader_data_graph() { 1243 ClassLoaderDataGraph::dump_on(tty); 1244 return 0; 1245 } 1246 1247 void ClassLoaderDataGraph::verify() { 1248 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) { 1249 data->verify(); 1250 } 1251 } 1252 1253 void ClassLoaderDataGraph::dump_on(outputStream * const out) { 1254 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) { 1255 data->dump(out); 1256 } 1257 MetaspaceAux::dump(out); 1258 } 1259 #endif // PRODUCT 1260 1261 void ClassLoaderData::print_value_on(outputStream* out) const { 1262 if (class_loader() == NULL) { 1263 out->print("NULL class_loader"); 1264 } else { 1265 out->print("class loader " INTPTR_FORMAT " ", p2i(this)); 1266 class_loader()->print_value_on(out); 1267 } 1268 } 1269 1270 #if INCLUDE_TRACE 1271 1272 Ticks ClassLoaderDataGraph::_class_unload_time; 1273 1274 void ClassLoaderDataGraph::class_unload_event(Klass* const k) { 1275 assert(k != NULL, "invariant"); 1276 1277 // post class unload event 1278 EventClassUnload event(UNTIMED); 1279 event.set_endtime(_class_unload_time); 1280 event.set_unloadedClass(k); 1281 event.set_definingClassLoader(k->class_loader_data()); 1282 event.commit(); 1283 } 1284 1285 #endif // INCLUDE_TRACE