1 /* 2 * Copyright (c) 2012, 2013, 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 // extension, 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/systemDictionary.hpp" 55 #include "code/codeCache.hpp" 56 #include "memory/metadataFactory.hpp" 57 #include "memory/metaspaceShared.hpp" 58 #include "memory/oopFactory.hpp" 59 #include "runtime/jniHandles.hpp" 60 #include "runtime/mutex.hpp" 61 #include "runtime/safepoint.hpp" 62 #include "runtime/synchronizer.hpp" 63 #include "utilities/growableArray.hpp" 64 #include "utilities/ostream.hpp" 65 66 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL; 67 68 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous) : 69 _class_loader(h_class_loader()), 70 _is_anonymous(is_anonymous), _keep_alive(is_anonymous), // initially 71 _metaspace(NULL), _unloading(false), _klasses(NULL), 72 _claimed(0), _jmethod_ids(NULL), _handles(NULL), _deallocate_list(NULL), 73 _next(NULL), _dependencies(NULL), 74 _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) { 75 // empty 76 } 77 78 void ClassLoaderData::init_dependencies(TRAPS) { 79 // Create empty dependencies array to add to. CMS requires this to be 80 // an oop so that it can track additions via card marks. We think. 81 _dependencies = (oop)oopFactory::new_objectArray(2, CHECK); 82 } 83 84 bool ClassLoaderData::claim() { 85 if (_claimed == 1) { 86 return false; 87 } 88 89 return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0; 90 } 91 92 void ClassLoaderData::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) { 93 if (must_claim && !claim()) { 94 return; 95 } 96 97 f->do_oop(&_class_loader); 98 f->do_oop(&_dependencies); 99 _handles->oops_do(f); 100 if (klass_closure != NULL) { 101 classes_do(klass_closure); 102 } 103 } 104 105 void ClassLoaderData::classes_do(KlassClosure* klass_closure) { 106 for (Klass* k = _klasses; k != NULL; k = k->next_link()) { 107 klass_closure->do_klass(k); 108 assert(k != k->next_link(), "no loops!"); 109 } 110 } 111 112 void ClassLoaderData::classes_do(void f(InstanceKlass*)) { 113 for (Klass* k = _klasses; k != NULL; k = k->next_link()) { 114 if (k->oop_is_instance()) { 115 f(InstanceKlass::cast(k)); 116 } 117 assert(k != k->next_link(), "no loops!"); 118 } 119 } 120 121 void ClassLoaderData::record_dependency(Klass* k, TRAPS) { 122 ClassLoaderData * const from_cld = this; 123 ClassLoaderData * const to_cld = k->class_loader_data(); 124 125 // Dependency to the null class loader data doesn't need to be recorded 126 // because the null class loader data never goes away. 127 if (to_cld->is_the_null_class_loader_data()) { 128 return; 129 } 130 131 oop to; 132 if (to_cld->is_anonymous()) { 133 // Anonymous class dependencies are through the mirror. 134 to = k->java_mirror(); 135 } else { 136 to = to_cld->class_loader(); 137 138 // If from_cld is anonymous, even if it's class_loader is a parent of 'to' 139 // we still have to add it. The class_loader won't keep from_cld alive. 140 if (!from_cld->is_anonymous()) { 141 // Check that this dependency isn't from the same or parent class_loader 142 oop from = from_cld->class_loader(); 143 144 oop curr = from; 145 while (curr != NULL) { 146 if (curr == to) { 147 return; // this class loader is in the parent list, no need to add it. 148 } 149 curr = java_lang_ClassLoader::parent(curr); 150 } 151 } 152 } 153 154 // It's a dependency we won't find through GC, add it. This is relatively rare 155 // Must handle over GC point. 156 Handle dependency(THREAD, to); 157 from_cld->add_dependency(dependency, CHECK); 158 } 159 160 161 void ClassLoaderData::add_dependency(Handle dependency, TRAPS) { 162 // Check first if this dependency is already in the list. 163 // Save a pointer to the last to add to under the lock. 164 objArrayOop ok = (objArrayOop)_dependencies; 165 objArrayOop last = NULL; 166 while (ok != NULL) { 167 last = ok; 168 if (ok->obj_at(0) == dependency()) { 169 // Don't need to add it 170 return; 171 } 172 ok = (objArrayOop)ok->obj_at(1); 173 } 174 175 // Must handle over GC points 176 assert (last != NULL, "dependencies should be initialized"); 177 objArrayHandle last_handle(THREAD, last); 178 179 // Create a new dependency node with fields for (class_loader or mirror, next) 180 objArrayOop deps = oopFactory::new_objectArray(2, CHECK); 181 deps->obj_at_put(0, dependency()); 182 183 // Must handle over GC points 184 objArrayHandle new_dependency(THREAD, deps); 185 186 // Add the dependency under lock 187 locked_add_dependency(last_handle, new_dependency); 188 } 189 190 void ClassLoaderData::locked_add_dependency(objArrayHandle last_handle, 191 objArrayHandle new_dependency) { 192 193 // Have to lock and put the new dependency on the end of the dependency 194 // array so the card mark for CMS sees that this dependency is new. 195 // Can probably do this lock free with some effort. 196 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); 197 198 oop loader_or_mirror = new_dependency->obj_at(0); 199 200 // Since the dependencies are only added, add to the end. 201 objArrayOop end = last_handle(); 202 objArrayOop last = NULL; 203 while (end != NULL) { 204 last = end; 205 // check again if another thread added it to the end. 206 if (end->obj_at(0) == loader_or_mirror) { 207 // Don't need to add it 208 return; 209 } 210 end = (objArrayOop)end->obj_at(1); 211 } 212 assert (last != NULL, "dependencies should be initialized"); 213 // fill in the first element with the oop in new_dependency. 214 if (last->obj_at(0) == NULL) { 215 last->obj_at_put(0, new_dependency->obj_at(0)); 216 } else { 217 last->obj_at_put(1, new_dependency()); 218 } 219 } 220 221 void ClassLoaderDataGraph::clear_claimed_marks() { 222 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { 223 cld->clear_claimed(); 224 } 225 } 226 227 void ClassLoaderData::add_class(Klass* k) { 228 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); 229 Klass* old_value = _klasses; 230 k->set_next_link(old_value); 231 // link the new item into the list 232 _klasses = k; 233 234 if (TraceClassLoaderData && Verbose && k->class_loader_data() != NULL) { 235 ResourceMark rm; 236 tty->print_cr("[TraceClassLoaderData] Adding k: " PTR_FORMAT " %s to CLD: " 237 PTR_FORMAT " loader: " PTR_FORMAT " %s", 238 k, 239 k->external_name(), 240 k->class_loader_data(), 241 k->class_loader(), 242 loader_name()); 243 } 244 } 245 246 // This is called by InstanceKlass::deallocate_contents() to remove the 247 // scratch_class for redefine classes. We need a lock because there it may not 248 // be called at a safepoint if there's an error. 249 void ClassLoaderData::remove_class(Klass* scratch_class) { 250 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); 251 Klass* prev = NULL; 252 for (Klass* k = _klasses; k != NULL; k = k->next_link()) { 253 if (k == scratch_class) { 254 if (prev == NULL) { 255 _klasses = k->next_link(); 256 } else { 257 Klass* next = k->next_link(); 258 prev->set_next_link(next); 259 } 260 return; 261 } 262 prev = k; 263 assert(k != k->next_link(), "no loops!"); 264 } 265 ShouldNotReachHere(); // should have found this class!! 266 } 267 268 void ClassLoaderData::unload() { 269 _unloading = true; 270 271 if (TraceClassLoaderData) { 272 ResourceMark rm; 273 tty->print("[ClassLoaderData: unload loader data "PTR_FORMAT, this); 274 tty->print(" for instance "PTR_FORMAT" of %s", class_loader(), 275 loader_name()); 276 if (is_anonymous()) { 277 tty->print(" for anonymous class "PTR_FORMAT " ", _klasses); 278 } 279 tty->print_cr("]"); 280 } 281 } 282 283 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const { 284 bool alive = 285 is_anonymous() ? 286 is_alive_closure->do_object_b(_klasses->java_mirror()) : 287 class_loader() == NULL || is_alive_closure->do_object_b(class_loader()); 288 assert(!alive || claimed(), "must be claimed"); 289 return alive; 290 } 291 292 293 ClassLoaderData::~ClassLoaderData() { 294 Metaspace *m = _metaspace; 295 if (m != NULL) { 296 _metaspace = NULL; 297 // release the metaspace 298 delete m; 299 // release the handles 300 if (_handles != NULL) { 301 JNIHandleBlock::release_block(_handles); 302 _handles = NULL; 303 } 304 } 305 306 // Clear all the JNI handles for methods 307 // These aren't deallocated and are going to look like a leak, but that's 308 // needed because we can't really get rid of jmethodIDs because we don't 309 // know when native code is going to stop using them. The spec says that 310 // they're "invalid" but existing programs likely rely on their being 311 // NULL after class unloading. 312 if (_jmethod_ids != NULL) { 313 Method::clear_jmethod_ids(this); 314 } 315 // Delete lock 316 delete _metaspace_lock; 317 318 // Delete free list 319 if (_deallocate_list != NULL) { 320 delete _deallocate_list; 321 } 322 } 323 324 /** 325 * Returns true if this class loader data is for the extension class loader. 326 */ 327 bool ClassLoaderData::is_ext_class_loader_data() const { 328 return SystemDictionary::is_ext_class_loader(class_loader()); 329 } 330 331 Metaspace* ClassLoaderData::metaspace_non_null() { 332 assert(!DumpSharedSpaces, "wrong metaspace!"); 333 // If the metaspace has not been allocated, create a new one. Might want 334 // to create smaller arena for Reflection class loaders also. 335 // The reason for the delayed allocation is because some class loaders are 336 // simply for delegating with no metadata of their own. 337 if (_metaspace == NULL) { 338 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); 339 // Check again if metaspace has been allocated while we were getting this lock. 340 if (_metaspace != NULL) { 341 return _metaspace; 342 } 343 if (this == the_null_class_loader_data()) { 344 assert (class_loader() == NULL, "Must be"); 345 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType)); 346 } else if (is_anonymous()) { 347 if (TraceClassLoaderData && Verbose && class_loader() != NULL) { 348 tty->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name()); 349 } 350 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType)); 351 } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) { 352 if (TraceClassLoaderData && Verbose && class_loader() != NULL) { 353 tty->print_cr("is_reflection: %s", class_loader()->klass()->internal_name()); 354 } 355 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType)); 356 } else { 357 set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType)); 358 } 359 } 360 return _metaspace; 361 } 362 363 JNIHandleBlock* ClassLoaderData::handles() const { return _handles; } 364 void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; } 365 366 jobject ClassLoaderData::add_handle(Handle h) { 367 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); 368 if (handles() == NULL) { 369 set_handles(JNIHandleBlock::allocate_block()); 370 } 371 return handles()->allocate_handle(h()); 372 } 373 374 // Add this metadata pointer to be freed when it's safe. This is only during 375 // class unloading because Handles might point to this metadata field. 376 void ClassLoaderData::add_to_deallocate_list(Metadata* m) { 377 // Metadata in shared region isn't deleted. 378 if (!m->is_shared()) { 379 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); 380 if (_deallocate_list == NULL) { 381 _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true); 382 } 383 _deallocate_list->append_if_missing(m); 384 } 385 } 386 387 // Deallocate free metadata on the free list. How useful the PermGen was! 388 void ClassLoaderData::free_deallocate_list() { 389 // Don't need lock, at safepoint 390 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint"); 391 if (_deallocate_list == NULL) { 392 return; 393 } 394 // Go backwards because this removes entries that are freed. 395 for (int i = _deallocate_list->length() - 1; i >= 0; i--) { 396 Metadata* m = _deallocate_list->at(i); 397 if (!m->on_stack()) { 398 _deallocate_list->remove_at(i); 399 // There are only three types of metadata that we deallocate directly. 400 // Cast them so they can be used by the template function. 401 if (m->is_method()) { 402 MetadataFactory::free_metadata(this, (Method*)m); 403 } else if (m->is_constantPool()) { 404 MetadataFactory::free_metadata(this, (ConstantPool*)m); 405 } else if (m->is_klass()) { 406 MetadataFactory::free_metadata(this, (InstanceKlass*)m); 407 } else { 408 ShouldNotReachHere(); 409 } 410 } 411 } 412 } 413 414 // These anonymous class loaders are to contain classes used for JSR292 415 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(oop loader, TRAPS) { 416 // Add a new class loader data to the graph. 417 return ClassLoaderDataGraph::add(NULL, loader, CHECK_NULL); 418 } 419 420 const char* ClassLoaderData::loader_name() { 421 // Handles null class loader 422 return SystemDictionary::loader_name(class_loader()); 423 } 424 425 #ifndef PRODUCT 426 // Define to dump klasses 427 #undef CLD_DUMP_KLASSES 428 429 void ClassLoaderData::dump(outputStream * const out) { 430 ResourceMark rm; 431 out->print("ClassLoaderData CLD: "PTR_FORMAT", loader: "PTR_FORMAT", loader_klass: "PTR_FORMAT" %s {", 432 this, class_loader(), 433 class_loader() != NULL ? class_loader()->klass() : NULL, loader_name()); 434 if (claimed()) out->print(" claimed "); 435 if (is_unloading()) out->print(" unloading "); 436 out->print(" handles " INTPTR_FORMAT, handles()); 437 out->cr(); 438 if (metaspace_or_null() != NULL) { 439 out->print_cr("metaspace: " PTR_FORMAT, metaspace_or_null()); 440 metaspace_or_null()->dump(out); 441 } else { 442 out->print_cr("metaspace: NULL"); 443 } 444 445 #ifdef CLD_DUMP_KLASSES 446 if (Verbose) { 447 ResourceMark rm; 448 Klass* k = _klasses; 449 while (k != NULL) { 450 out->print_cr("klass "PTR_FORMAT", %s, CT: %d, MUT: %d", k, k->name()->as_C_string(), 451 k->has_modified_oops(), k->has_accumulated_modified_oops()); 452 assert(k != k->next_link(), "no loops!"); 453 k = k->next_link(); 454 } 455 } 456 #endif // CLD_DUMP_KLASSES 457 #undef CLD_DUMP_KLASSES 458 if (_jmethod_ids != NULL) { 459 Method::print_jmethod_ids(this, out); 460 } 461 out->print_cr("}"); 462 } 463 #endif // PRODUCT 464 465 void ClassLoaderData::verify() { 466 oop cl = class_loader(); 467 468 guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same"); 469 guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be"); 470 471 // Verify the integrity of the allocated space. 472 if (metaspace_or_null() != NULL) { 473 metaspace_or_null()->verify(); 474 } 475 476 for (Klass* k = _klasses; k != NULL; k = k->next_link()) { 477 guarantee(k->class_loader_data() == this, "Must be the same"); 478 k->verify(); 479 assert(k != k->next_link(), "no loops!"); 480 } 481 } 482 483 484 // GC root of class loader data created. 485 ClassLoaderData* ClassLoaderDataGraph::_head = NULL; 486 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL; 487 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL; 488 489 490 // Add a new class loader data node to the list. Assign the newly created 491 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field 492 ClassLoaderData* ClassLoaderDataGraph::add(ClassLoaderData** cld_addr, Handle loader, TRAPS) { 493 // Not assigned a class loader data yet. 494 // Create one. 495 ClassLoaderData* *list_head = &_head; 496 ClassLoaderData* next = _head; 497 498 bool is_anonymous = (cld_addr == NULL); 499 ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous); 500 501 if (cld_addr != NULL) { 502 // First, Atomically set it 503 ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL); 504 if (old != NULL) { 505 delete cld; 506 // Returns the data. 507 return old; 508 } 509 } 510 511 // We won the race, and therefore the task of adding the data to the list of 512 // class loader data 513 do { 514 cld->set_next(next); 515 ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next); 516 if (exchanged == next) { 517 if (TraceClassLoaderData) { 518 ResourceMark rm; 519 tty->print("[ClassLoaderData: "); 520 tty->print("create class loader data "PTR_FORMAT, cld); 521 tty->print(" for instance "PTR_FORMAT" of %s", cld->class_loader(), 522 cld->loader_name()); 523 tty->print_cr("]"); 524 } 525 // Create dependencies after the CLD is added to the list. Otherwise, 526 // the GC GC will not find the CLD and the _class_loader field will 527 // not be updated. 528 cld->init_dependencies(CHECK_NULL); 529 return cld; 530 } 531 next = exchanged; 532 } while (true); 533 534 } 535 536 void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) { 537 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { 538 cld->oops_do(f, klass_closure, must_claim); 539 } 540 } 541 542 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) { 543 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { 544 if (cld->keep_alive()) { 545 cld->oops_do(f, klass_closure, must_claim); 546 } 547 } 548 } 549 550 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) { 551 if (ClassUnloading) { 552 ClassLoaderData::the_null_class_loader_data()->oops_do(f, klass_closure, must_claim); 553 // keep any special CLDs alive. 554 ClassLoaderDataGraph::keep_alive_oops_do(f, klass_closure, must_claim); 555 } else { 556 ClassLoaderDataGraph::oops_do(f, klass_closure, must_claim); 557 } 558 } 559 560 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) { 561 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { 562 cld->classes_do(klass_closure); 563 } 564 } 565 566 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() { 567 assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?"); 568 569 GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>(); 570 571 // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true); 572 ClassLoaderData* curr = _head; 573 while (curr != _saved_head) { 574 if (!curr->claimed()) { 575 array->push(curr); 576 577 if (TraceClassLoaderData) { 578 tty->print("[ClassLoaderData] found new CLD: "); 579 curr->print_value_on(tty); 580 tty->cr(); 581 } 582 } 583 584 curr = curr->_next; 585 } 586 587 return array; 588 } 589 590 #ifndef PRODUCT 591 // for debugging and hsfind(x) 592 bool ClassLoaderDataGraph::contains(address x) { 593 // I think we need the _metaspace_lock taken here because the class loader 594 // data graph could be changing while we are walking it (new entries added, 595 // new entries being unloaded, etc). 596 if (DumpSharedSpaces) { 597 // There are only two metaspaces to worry about. 598 ClassLoaderData* ncld = ClassLoaderData::the_null_class_loader_data(); 599 return (ncld->ro_metaspace()->contains(x) || ncld->rw_metaspace()->contains(x)); 600 } 601 602 if (UseSharedSpaces && MetaspaceShared::is_in_shared_space(x)) { 603 return true; 604 } 605 606 for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { 607 if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) { 608 return true; 609 } 610 } 611 612 // Could also be on an unloading list which is okay, ie. still allocated 613 // for a little while. 614 for (ClassLoaderData* ucld = _unloading; ucld != NULL; ucld = ucld->next()) { 615 if (ucld->metaspace_or_null() != NULL && ucld->metaspace_or_null()->contains(x)) { 616 return true; 617 } 618 } 619 return false; 620 } 621 622 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) { 623 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) { 624 if (loader_data == data) { 625 return true; 626 } 627 } 628 629 return false; 630 } 631 #endif // PRODUCT 632 633 634 // Move class loader data from main list to the unloaded list for unloading 635 // and deallocation later. 636 bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive_closure) { 637 ClassLoaderData* data = _head; 638 ClassLoaderData* prev = NULL; 639 bool seen_dead_loader = false; 640 // mark metadata seen on the stack and code cache so we can delete 641 // unneeded entries. 642 bool has_redefined_a_class = JvmtiExport::has_redefined_a_class(); 643 MetadataOnStackMark md_on_stack; 644 while (data != NULL) { 645 if (data->keep_alive() || data->is_alive(is_alive_closure)) { 646 if (has_redefined_a_class) { 647 data->classes_do(InstanceKlass::purge_previous_versions); 648 } 649 data->free_deallocate_list(); 650 prev = data; 651 data = data->next(); 652 continue; 653 } 654 seen_dead_loader = true; 655 ClassLoaderData* dead = data; 656 dead->unload(); 657 data = data->next(); 658 // Remove from loader list. 659 if (prev != NULL) { 660 prev->set_next(data); 661 } else { 662 assert(dead == _head, "sanity check"); 663 _head = data; 664 } 665 dead->set_next(_unloading); 666 _unloading = dead; 667 } 668 return seen_dead_loader; 669 } 670 671 void ClassLoaderDataGraph::purge() { 672 ClassLoaderData* list = _unloading; 673 _unloading = NULL; 674 ClassLoaderData* next = list; 675 while (next != NULL) { 676 ClassLoaderData* purge_me = next; 677 next = purge_me->next(); 678 delete purge_me; 679 } 680 } 681 682 // CDS support 683 684 // Global metaspaces for writing information to the shared archive. When 685 // application CDS is supported, we may need one per metaspace, so this 686 // sort of looks like it. 687 Metaspace* ClassLoaderData::_ro_metaspace = NULL; 688 Metaspace* ClassLoaderData::_rw_metaspace = NULL; 689 static bool _shared_metaspaces_initialized = false; 690 691 // Initialize shared metaspaces (change to call from somewhere not lazily) 692 void ClassLoaderData::initialize_shared_metaspaces() { 693 assert(DumpSharedSpaces, "only use this for dumping shared spaces"); 694 assert(this == ClassLoaderData::the_null_class_loader_data(), 695 "only supported for null loader data for now"); 696 assert (!_shared_metaspaces_initialized, "only initialize once"); 697 MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); 698 _ro_metaspace = new Metaspace(_metaspace_lock, Metaspace::ROMetaspaceType); 699 _rw_metaspace = new Metaspace(_metaspace_lock, Metaspace::ReadWriteMetaspaceType); 700 _shared_metaspaces_initialized = true; 701 } 702 703 Metaspace* ClassLoaderData::ro_metaspace() { 704 assert(_ro_metaspace != NULL, "should already be initialized"); 705 return _ro_metaspace; 706 } 707 708 Metaspace* ClassLoaderData::rw_metaspace() { 709 assert(_rw_metaspace != NULL, "should already be initialized"); 710 return _rw_metaspace; 711 } 712 713 714 ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() { 715 _data = ClassLoaderDataGraph::_head; 716 } 717 718 ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {} 719 720 #ifndef PRODUCT 721 // callable from debugger 722 extern "C" int print_loader_data_graph() { 723 ClassLoaderDataGraph::dump_on(tty); 724 return 0; 725 } 726 727 void ClassLoaderDataGraph::verify() { 728 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) { 729 data->verify(); 730 } 731 } 732 733 void ClassLoaderDataGraph::dump_on(outputStream * const out) { 734 for (ClassLoaderData* data = _head; data != NULL; data = data->next()) { 735 data->dump(out); 736 } 737 MetaspaceAux::dump(out); 738 } 739 #endif // PRODUCT 740 741 void ClassLoaderData::print_value_on(outputStream* out) const { 742 if (class_loader() == NULL) { 743 out->print("NULL class_loader"); 744 } else { 745 out->print("class loader "PTR_FORMAT, this); 746 class_loader()->print_value_on(out); 747 } 748 }