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