coleenp@4037: /* coleenp@4037: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. coleenp@4037: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. coleenp@4037: * coleenp@4037: * This code is free software; you can redistribute it and/or modify it coleenp@4037: * under the terms of the GNU General Public License version 2 only, as coleenp@4037: * published by the Free Software Foundation. coleenp@4037: * coleenp@4037: * This code is distributed in the hope that it will be useful, but WITHOUT coleenp@4037: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or coleenp@4037: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License coleenp@4037: * version 2 for more details (a copy is included in the LICENSE file that coleenp@4037: * accompanied this code). coleenp@4037: * coleenp@4037: * You should have received a copy of the GNU General Public License version coleenp@4037: * 2 along with this work; if not, write to the Free Software Foundation, coleenp@4037: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. coleenp@4037: * coleenp@4037: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA coleenp@4037: * or visit www.oracle.com if you need additional information or have any coleenp@4037: * questions. coleenp@4037: * coleenp@4037: */ coleenp@4037: coleenp@4037: // A ClassLoaderData identifies the full set of class types that a class coleenp@4037: // loader's name resolution strategy produces for a given configuration of the coleenp@4037: // class loader. coleenp@4037: // Class types in the ClassLoaderData may be defined by from class file binaries coleenp@4037: // provided by the class loader, or from other class loader it interacts with coleenp@4037: // according to its name resolution strategy. coleenp@4037: // coleenp@4037: // Class loaders that implement a deterministic name resolution strategy coleenp@4037: // (including with respect to their delegation behavior), such as the boot, the coleenp@4037: // extension, and the system loaders of the JDK's built-in class loader coleenp@4037: // hierarchy, always produce the same linkset for a given configuration. coleenp@4037: // coleenp@4037: // ClassLoaderData carries information related to a linkset (e.g., coleenp@4037: // metaspace holding its klass definitions). coleenp@4037: // The System Dictionary and related data structures (e.g., placeholder table, coleenp@4037: // loader constraints table) as well as the runtime representation of classes coleenp@4037: // only reference ClassLoaderData. coleenp@4037: // coleenp@4037: // Instances of java.lang.ClassLoader holds a pointer to a ClassLoaderData that coleenp@4037: // that represent the loader's "linking domain" in the JVM. coleenp@4037: // coleenp@4037: // The bootstrap loader (represented by NULL) also has a ClassLoaderData, coleenp@4037: // the singleton class the_null_class_loader_data(). coleenp@4037: coleenp@4037: #include "precompiled.hpp" coleenp@4037: #include "classfile/classLoaderData.hpp" coleenp@4037: #include "classfile/classLoaderData.inline.hpp" coleenp@4037: #include "classfile/javaClasses.hpp" coleenp@4037: #include "classfile/systemDictionary.hpp" coleenp@4037: #include "code/codeCache.hpp" coleenp@4037: #include "memory/metadataFactory.hpp" coleenp@4037: #include "memory/metaspaceShared.hpp" coleenp@4037: #include "prims/jvmtiRedefineClasses.hpp" coleenp@4037: #include "runtime/jniHandles.hpp" coleenp@4037: #include "runtime/mutex.hpp" coleenp@4037: #include "runtime/safepoint.hpp" coleenp@4037: #include "runtime/synchronizer.hpp" coleenp@4037: #include "utilities/growableArray.hpp" coleenp@4037: #include "utilities/ostream.hpp" coleenp@4037: coleenp@4037: ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL; coleenp@4037: coleenp@4037: ClassLoaderData::ClassLoaderData(Handle h_class_loader) : _class_loader(h_class_loader()), coleenp@4037: _metaspace(NULL), _unloading(false), _klasses(NULL), coleenp@4037: _claimed(0), _jmethod_ids(NULL), _handles(NULL), coleenp@4037: _deallocate_list(NULL), _next(NULL), coleenp@4037: _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) { coleenp@4037: // empty coleenp@4037: } coleenp@4037: coleenp@4037: bool ClassLoaderData::claim() { coleenp@4037: if (_claimed == 1) { coleenp@4037: return false; coleenp@4037: } coleenp@4037: coleenp@4037: return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0; coleenp@4037: } coleenp@4037: coleenp@4037: void ClassLoaderData::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) { coleenp@4037: if (must_claim && !claim()) { coleenp@4037: return; coleenp@4037: } coleenp@4037: coleenp@4037: f->do_oop(&_class_loader); coleenp@4037: _handles->oops_do(f); coleenp@4037: if (klass_closure != NULL) { coleenp@4037: classes_do(klass_closure); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: void ClassLoaderData::classes_do(KlassClosure* klass_closure) { coleenp@4037: for (Klass* k = _klasses; k != NULL; k = k->next_link()) { coleenp@4037: klass_closure->do_klass(k); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: void ClassLoaderData::classes_do(void f(InstanceKlass*)) { coleenp@4037: for (Klass* k = _klasses; k != NULL; k = k->next_link()) { coleenp@4037: if (k->oop_is_instance()) { coleenp@4037: f(InstanceKlass::cast(k)); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: void ClassLoaderData::record_dependency(Klass* k, TRAPS) { coleenp@4037: ClassLoaderData * const from_cld = this; coleenp@4037: ClassLoaderData * const to_cld = k->class_loader_data(); coleenp@4037: coleenp@4037: // Records dependency between non-null class loaders only. coleenp@4037: if (to_cld->is_the_null_class_loader_data() || from_cld->is_the_null_class_loader_data()) { coleenp@4037: return; coleenp@4037: } coleenp@4037: coleenp@4037: // Check that this dependency isn't from the same or parent class_loader coleenp@4037: oop to = to_cld->class_loader(); coleenp@4037: oop from = from_cld->class_loader(); coleenp@4037: coleenp@4037: oop curr = from; coleenp@4037: while (curr != NULL) { coleenp@4037: if (curr == to) { coleenp@4037: return; // this class loader is in the parent list, no need to add it. coleenp@4037: } coleenp@4037: curr = java_lang_ClassLoader::parent(curr); coleenp@4037: } coleenp@4037: coleenp@4037: // It's a dependency we won't find through GC, add it. This is relatively rare coleenp@4037: from_cld->add_dependency(to_cld, CHECK); coleenp@4037: } coleenp@4037: coleenp@4037: bool ClassLoaderData::has_dependency(ClassLoaderData* dependency) { coleenp@4037: oop loader = dependency->class_loader(); coleenp@4037: coleenp@4037: // Get objArrayOop out of the class_loader oop and see if this dependency coleenp@4037: // is there. Don't safepoint! These are all oops. coleenp@4037: // Dependency list is (oop class_loader, objArrayOop next) coleenp@4037: objArrayOop ok = (objArrayOop)java_lang_ClassLoader::dependencies(class_loader()); coleenp@4037: while (ok != NULL) { coleenp@4037: if (ok->obj_at(0) == loader) { coleenp@4037: return true; coleenp@4037: } coleenp@4037: ok = (objArrayOop)ok->obj_at(1); coleenp@4037: } coleenp@4037: return false; coleenp@4037: } coleenp@4037: coleenp@4037: void ClassLoaderData::add_dependency(ClassLoaderData* dependency, TRAPS) { coleenp@4037: // Minimize the number of duplicates in the list. coleenp@4037: if (has_dependency(dependency)) { coleenp@4037: return; coleenp@4037: } coleenp@4037: coleenp@4037: // Create a new dependency node with fields for (class_loader, next) coleenp@4037: objArrayOop deps = oopFactory::new_objectArray(2, CHECK); coleenp@4037: deps->obj_at_put(0, dependency->class_loader()); coleenp@4037: coleenp@4037: // Add this lock free, using compare and exchange, need barriers for GC coleenp@4037: // Do the barrier first. coleenp@4037: HeapWord* addr = java_lang_ClassLoader::dependencies_addr(class_loader()); coleenp@4037: while (true) { coleenp@4037: oop old_dependency = java_lang_ClassLoader::dependencies(class_loader()); coleenp@4037: deps->obj_at_put(1, old_dependency); coleenp@4037: coleenp@4037: oop newold = oopDesc::atomic_compare_exchange_oop((oop)deps, addr, old_dependency, true); coleenp@4037: if (newold == old_dependency) { coleenp@4037: update_barrier_set((void*)addr, (oop)deps); coleenp@4037: // we won the race to add this dependency coleenp@4037: break; coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: coleenp@4037: void ClassLoaderDataGraph::clear_claimed_marks() { coleenp@4037: for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { coleenp@4037: cld->clear_claimed(); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: void ClassLoaderData::add_class(Klass* k) { coleenp@4037: MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: Klass* old_value = _klasses; coleenp@4037: k->set_next_link(old_value); coleenp@4037: // link the new item into the list coleenp@4037: _klasses = k; coleenp@4037: coleenp@4037: if (TraceClassLoaderData && k->class_loader_data() != NULL) { coleenp@4037: ResourceMark rm; coleenp@4037: tty->print_cr("[TraceClassLoaderData] Adding k: " PTR_FORMAT " %s to CLD: " coleenp@4037: PTR_FORMAT " loader: " PTR_FORMAT " %s", coleenp@4037: k, coleenp@4037: k->external_name(), coleenp@4037: k->class_loader_data(), coleenp@4037: k->class_loader(), coleenp@4037: k->class_loader() != NULL ? k->class_loader()->klass()->external_name() : "NULL" coleenp@4037: ); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // This is called by InstanceKlass::deallocate_contents() to remove the coleenp@4037: // scratch_class for redefine classes. We need a lock because there it may not coleenp@4037: // be called at a safepoint if there's an error. coleenp@4037: void ClassLoaderData::remove_class(Klass* scratch_class) { coleenp@4037: MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: Klass* prev = NULL; coleenp@4037: for (Klass* k = _klasses; k != NULL; k = k->next_link()) { coleenp@4037: if (k == scratch_class) { coleenp@4037: if (prev == NULL) { coleenp@4037: _klasses = k->next_link(); coleenp@4037: } else { coleenp@4037: Klass* next = k->next_link(); coleenp@4037: prev->set_next_link(next); coleenp@4037: } coleenp@4037: return; coleenp@4037: } coleenp@4037: prev = k; coleenp@4037: } coleenp@4037: ShouldNotReachHere(); // should have found this class!! coleenp@4037: } coleenp@4037: coleenp@4037: ClassLoaderData::~ClassLoaderData() { coleenp@4037: Metaspace *m = _metaspace; coleenp@4037: if (m != NULL) { coleenp@4037: _metaspace = NULL; coleenp@4037: // release the metaspace coleenp@4037: delete m; coleenp@4037: // release the handles coleenp@4037: if (_handles != NULL) { coleenp@4037: JNIHandleBlock::release_block(_handles); coleenp@4037: _handles = NULL; coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // Clear all the JNI handles for methods coleenp@4037: // These aren't deallocated and are going to look like a leak, but that's coleenp@4037: // needed because we can't really get rid of jmethodIDs because we don't coleenp@4037: // know when native code is going to stop using them. The spec says that coleenp@4037: // they're "invalid" but existing programs likely rely on their being coleenp@4037: // NULL after class unloading. coleenp@4037: if (_jmethod_ids != NULL) { coleenp@4037: Method::clear_jmethod_ids(this); coleenp@4037: } coleenp@4037: // Delete lock coleenp@4037: delete _metaspace_lock; coleenp@4037: coleenp@4037: // Delete free list coleenp@4037: if (_deallocate_list != NULL) { coleenp@4037: delete _deallocate_list; coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: Metaspace* ClassLoaderData::metaspace_non_null() { coleenp@4037: // If the metaspace has not been allocated, create a new one. Might want coleenp@4037: // to create smaller arena for Reflection class loaders also. coleenp@4037: // The reason for the delayed allocation is because some class loaders are coleenp@4037: // simply for delegating with no metadata of their own. coleenp@4037: if (_metaspace == NULL) { coleenp@4037: MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: // Check again if metaspace has been allocated while we were getting this lock. coleenp@4037: if (_metaspace != NULL) { coleenp@4037: return _metaspace; coleenp@4037: } coleenp@4037: if (class_loader() == NULL) { coleenp@4037: assert(this == the_null_class_loader_data(), "Must be"); coleenp@4037: size_t word_size = Metaspace::first_chunk_word_size(); coleenp@4037: set_metaspace(new Metaspace(_metaspace_lock, word_size)); coleenp@4037: } else { coleenp@4037: set_metaspace(new Metaspace(_metaspace_lock)); // default size for now. coleenp@4037: } coleenp@4037: } coleenp@4037: return _metaspace; coleenp@4037: } coleenp@4037: coleenp@4037: JNIHandleBlock* ClassLoaderData::handles() const { return _handles; } coleenp@4037: void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; } coleenp@4037: coleenp@4037: jobject ClassLoaderData::add_handle(Handle h) { coleenp@4037: MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: if (handles() == NULL) { coleenp@4037: set_handles(JNIHandleBlock::allocate_block()); coleenp@4037: } coleenp@4037: return handles()->allocate_handle(h()); coleenp@4037: } coleenp@4037: coleenp@4037: // Add this metadata pointer to be freed when it's safe. This is only during coleenp@4037: // class unloading because Handles might point to this metadata field. coleenp@4037: void ClassLoaderData::add_to_deallocate_list(Metadata* m) { coleenp@4037: // Metadata in shared region isn't deleted. coleenp@4037: if (!m->is_shared()) { coleenp@4037: MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: if (_deallocate_list == NULL) { coleenp@4037: _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray(100, true); coleenp@4037: } coleenp@4037: _deallocate_list->append_if_missing(m); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // Deallocate free metadata on the free list. How useful the PermGen was! coleenp@4037: void ClassLoaderData::free_deallocate_list() { coleenp@4037: // Don't need lock, at safepoint coleenp@4037: assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint"); coleenp@4037: if (_deallocate_list == NULL) { coleenp@4037: return; coleenp@4037: } coleenp@4037: // Go backwards because this removes entries that are freed. coleenp@4037: for (int i = _deallocate_list->length() - 1; i >= 0; i--) { coleenp@4037: Metadata* m = _deallocate_list->at(i); coleenp@4037: if (!m->on_stack()) { coleenp@4037: _deallocate_list->remove_at(i); coleenp@4037: // There are only three types of metadata that we deallocate directly. coleenp@4037: // Cast them so they can be used by the template function. coleenp@4037: if (m->is_method()) { coleenp@4037: MetadataFactory::free_metadata(this, (Method*)m); coleenp@4037: } else if (m->is_constantPool()) { coleenp@4037: MetadataFactory::free_metadata(this, (ConstantPool*)m); coleenp@4037: } else if (m->is_klass()) { coleenp@4037: MetadataFactory::free_metadata(this, (InstanceKlass*)m); coleenp@4037: } else { coleenp@4037: ShouldNotReachHere(); coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: #ifndef PRODUCT coleenp@4037: void ClassLoaderData::print_loader(ClassLoaderData *loader_data, outputStream* out) { coleenp@4037: oop class_loader = loader_data->class_loader(); coleenp@4037: out->print("%s", SystemDictionary::loader_name(class_loader)); coleenp@4037: } coleenp@4037: coleenp@4037: // Define to dump klasses coleenp@4037: #undef CLD_DUMP_KLASSES coleenp@4037: coleenp@4037: void ClassLoaderData::dump(outputStream * const out) { coleenp@4037: ResourceMark rm; coleenp@4037: out->print("ClassLoaderData CLD: "PTR_FORMAT", loader: "PTR_FORMAT", loader_klass: "PTR_FORMAT" %s {", coleenp@4037: this, class_loader(), coleenp@4037: class_loader() != NULL ? class_loader()->klass() : NULL, coleenp@4037: class_loader() != NULL ? class_loader()->klass()->external_name() : "NULL"); coleenp@4037: if (claimed()) out->print(" claimed "); coleenp@4037: if (is_unloading()) out->print(" unloading "); coleenp@4037: out->print(" handles " INTPTR_FORMAT, handles()); coleenp@4037: out->cr(); coleenp@4037: if (metaspace_or_null() != NULL) { coleenp@4037: out->print_cr("metaspace: " PTR_FORMAT, metaspace_or_null()); coleenp@4037: metaspace_or_null()->dump(out); coleenp@4037: } else { coleenp@4037: out->print_cr("metaspace: NULL"); coleenp@4037: } coleenp@4037: coleenp@4037: #ifdef CLD_DUMP_KLASSES coleenp@4037: if (Verbose) { coleenp@4037: ResourceMark rm; coleenp@4037: Klass* k = _klasses; coleenp@4037: while (k != NULL) { coleenp@4037: out->print_cr("klass "PTR_FORMAT", %s, CT: %d, MUT: %d", k, k->name()->as_C_string(), coleenp@4037: k->has_modified_oops(), k->has_accumulated_modified_oops()); coleenp@4037: k = k->next_link(); coleenp@4037: } coleenp@4037: } coleenp@4037: #endif // CLD_DUMP_KLASSES coleenp@4037: #undef CLD_DUMP_KLASSES coleenp@4037: if (_jmethod_ids != NULL) { coleenp@4037: Method::print_jmethod_ids(this, out); coleenp@4037: } coleenp@4037: out->print_cr("}"); coleenp@4037: } coleenp@4037: #endif // PRODUCT coleenp@4037: coleenp@4037: void ClassLoaderData::verify() { coleenp@4037: oop cl = class_loader(); coleenp@4037: coleenp@4037: guarantee(this == class_loader_data(cl), "Must be the same"); coleenp@4037: guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data(), "must be"); coleenp@4037: coleenp@4037: // Verify the integrity of the allocated space. coleenp@4037: if (metaspace_or_null() != NULL) { coleenp@4037: metaspace_or_null()->verify(); coleenp@4037: } coleenp@4037: coleenp@4037: for (Klass* k = _klasses; k != NULL; k = k->next_link()) { coleenp@4037: guarantee(k->class_loader_data() == this, "Must be the same"); coleenp@4037: k->verify(); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // GC root of class loader data created. coleenp@4037: ClassLoaderData* ClassLoaderDataGraph::_head = NULL; coleenp@4037: ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL; coleenp@4037: ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL; coleenp@4037: coleenp@4037: coleenp@4037: // Add a new class loader data node to the list. Assign the newly created coleenp@4037: // ClassLoaderData into the java/lang/ClassLoader object as a hidden field coleenp@4037: ClassLoaderData* ClassLoaderDataGraph::add(ClassLoaderData** cld_addr, Handle loader_data) { coleenp@4037: // Not assigned a class loader data yet. coleenp@4037: // Create one. coleenp@4037: ClassLoaderData* *list_head = &_head; coleenp@4037: ClassLoaderData* next = _head; coleenp@4037: ClassLoaderData* cld = new ClassLoaderData(loader_data); coleenp@4037: coleenp@4037: // First, Atomically set it. coleenp@4037: ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL); coleenp@4037: if (old != NULL) { coleenp@4037: delete cld; coleenp@4037: // Returns the data. coleenp@4037: return old; coleenp@4037: } coleenp@4037: coleenp@4037: // We won the race, and therefore the task of adding the data to the list of coleenp@4037: // class loader data coleenp@4037: do { coleenp@4037: cld->set_next(next); coleenp@4037: ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next); coleenp@4037: if (exchanged == next) { coleenp@4037: if (TraceClassLoaderData) { coleenp@4037: tty->print("[ClassLoaderData: "); coleenp@4037: tty->print("create class loader data "PTR_FORMAT, cld); coleenp@4037: tty->print(" for instance "PTR_FORMAT" of ", cld->class_loader()); coleenp@4037: loader_data->klass()->name()->print_symbol_on(tty); coleenp@4037: tty->print_cr("]"); coleenp@4037: } coleenp@4037: return cld; coleenp@4037: } coleenp@4037: next = exchanged; coleenp@4037: } while (true); coleenp@4037: } coleenp@4037: coleenp@4037: void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) { coleenp@4037: for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { coleenp@4037: cld->oops_do(f, klass_closure, must_claim); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) { coleenp@4037: if (ClassUnloading) { coleenp@4037: ClassLoaderData::the_null_class_loader_data()->oops_do(f, klass_closure, must_claim); coleenp@4037: } else { coleenp@4037: ClassLoaderDataGraph::oops_do(f, klass_closure, must_claim); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) { coleenp@4037: for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { coleenp@4037: cld->classes_do(klass_closure); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: GrowableArray* ClassLoaderDataGraph::new_clds() { coleenp@4037: assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?"); coleenp@4037: coleenp@4037: GrowableArray* array = new GrowableArray(); coleenp@4037: coleenp@4037: // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true); coleenp@4037: ClassLoaderData* curr = _head; coleenp@4037: while (curr != _saved_head) { coleenp@4037: if (!curr->claimed()) { coleenp@4037: array->push(curr); coleenp@4037: coleenp@4037: if (TraceClassLoaderData) { coleenp@4037: tty->print("[ClassLoaderData] found new CLD: "); coleenp@4037: curr->print_value_on(tty); coleenp@4037: tty->cr(); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: curr = curr->_next; coleenp@4037: } coleenp@4037: coleenp@4037: return array; coleenp@4037: } coleenp@4037: coleenp@4037: #ifndef PRODUCT coleenp@4037: // for debugging and hsfind(x) coleenp@4037: bool ClassLoaderDataGraph::contains(address x) { coleenp@4037: // I think we need the _metaspace_lock taken here because the class loader coleenp@4037: // data graph could be changing while we are walking it (new entries added, coleenp@4037: // new entries being unloaded, etc). coleenp@4037: if (DumpSharedSpaces) { coleenp@4037: // There are only two metaspaces to worry about. coleenp@4037: ClassLoaderData* ncld = ClassLoaderData::the_null_class_loader_data(); coleenp@4037: return (ncld->ro_metaspace()->contains(x) || ncld->rw_metaspace()->contains(x)); coleenp@4037: } coleenp@4037: coleenp@4037: if (UseSharedSpaces && MetaspaceShared::is_in_shared_space(x)) { coleenp@4037: return true; coleenp@4037: } coleenp@4037: coleenp@4037: for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { coleenp@4037: if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) { coleenp@4037: return true; coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // Could also be on an unloading list which is okay, ie. still allocated coleenp@4037: // for a little while. coleenp@4037: for (ClassLoaderData* ucld = _unloading; ucld != NULL; ucld = ucld->next()) { coleenp@4037: if (ucld->metaspace_or_null() != NULL && ucld->metaspace_or_null()->contains(x)) { coleenp@4037: return true; coleenp@4037: } coleenp@4037: } coleenp@4037: return false; coleenp@4037: } coleenp@4037: coleenp@4037: bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) { coleenp@4037: for (ClassLoaderData* data = _head; data != NULL; data = data->next()) { coleenp@4037: if (loader_data == data) { coleenp@4037: return true; coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: return false; coleenp@4037: } coleenp@4037: #endif // PRODUCT coleenp@4037: coleenp@4037: // Move class loader data from main list to the unloaded list for unloading coleenp@4037: // and deallocation later. coleenp@4037: bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive) { coleenp@4037: ClassLoaderData* data = _head; coleenp@4037: ClassLoaderData* prev = NULL; coleenp@4037: bool seen_dead_loader = false; coleenp@4037: // mark metadata seen on the stack and code cache so we can delete coleenp@4037: // unneeded entries. coleenp@4037: bool has_redefined_a_class = JvmtiExport::has_redefined_a_class(); coleenp@4037: MetadataOnStackMark md_on_stack; coleenp@4037: while (data != NULL) { coleenp@4037: if (data->class_loader() == NULL || is_alive->do_object_b(data->class_loader())) { coleenp@4037: assert(data->claimed(), "class loader data must have been claimed"); coleenp@4037: if (has_redefined_a_class) { coleenp@4037: data->classes_do(InstanceKlass::purge_previous_versions); coleenp@4037: } coleenp@4037: data->free_deallocate_list(); coleenp@4037: prev = data; coleenp@4037: data = data->next(); coleenp@4037: continue; coleenp@4037: } coleenp@4037: seen_dead_loader = true; coleenp@4037: ClassLoaderData* dead = data; coleenp@4037: dead->mark_for_unload(); coleenp@4037: if (TraceClassLoaderData) { coleenp@4037: tty->print("[ClassLoaderData: unload loader data "PTR_FORMAT, dead); coleenp@4037: tty->print(" for instance "PTR_FORMAT" of ", dead->class_loader()); coleenp@4037: dead->class_loader()->klass()->name()->print_symbol_on(tty); coleenp@4037: tty->print_cr("]"); coleenp@4037: } coleenp@4037: data = data->next(); coleenp@4037: // Remove from loader list. coleenp@4037: if (prev != NULL) { coleenp@4037: prev->set_next(data); coleenp@4037: } else { coleenp@4037: assert(dead == _head, "sanity check"); coleenp@4037: _head = data; coleenp@4037: } coleenp@4037: dead->set_next(_unloading); coleenp@4037: _unloading = dead; coleenp@4037: } coleenp@4037: return seen_dead_loader; coleenp@4037: } coleenp@4037: coleenp@4037: void ClassLoaderDataGraph::purge() { coleenp@4037: ClassLoaderData* list = _unloading; coleenp@4037: _unloading = NULL; coleenp@4037: ClassLoaderData* next = list; coleenp@4037: while (next != NULL) { coleenp@4037: ClassLoaderData* purge_me = next; coleenp@4037: next = purge_me->next(); coleenp@4037: delete purge_me; coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // CDS support coleenp@4037: coleenp@4037: // Global metaspaces for writing information to the shared archive. When coleenp@4037: // application CDS is supported, we may need one per metaspace, so this coleenp@4037: // sort of looks like it. coleenp@4037: Metaspace* ClassLoaderData::_ro_metaspace = NULL; coleenp@4037: Metaspace* ClassLoaderData::_rw_metaspace = NULL; coleenp@4037: static bool _shared_metaspaces_initialized = false; coleenp@4037: coleenp@4037: // Initialize shared metaspaces (change to call from somewhere not lazily) coleenp@4037: void ClassLoaderData::initialize_shared_metaspaces() { coleenp@4037: assert(DumpSharedSpaces, "only use this for dumping shared spaces"); coleenp@4037: assert(this == ClassLoaderData::the_null_class_loader_data(), coleenp@4037: "only supported for null loader data for now"); coleenp@4037: assert (!_shared_metaspaces_initialized, "only initialize once"); coleenp@4037: MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag); coleenp@4037: _ro_metaspace = new Metaspace(_metaspace_lock, SharedReadOnlySize/wordSize); coleenp@4037: _rw_metaspace = new Metaspace(_metaspace_lock, SharedReadWriteSize/wordSize); coleenp@4037: _shared_metaspaces_initialized = true; coleenp@4037: } coleenp@4037: coleenp@4037: Metaspace* ClassLoaderData::ro_metaspace() { coleenp@4037: assert(_ro_metaspace != NULL, "should already be initialized"); coleenp@4037: return _ro_metaspace; coleenp@4037: } coleenp@4037: coleenp@4037: Metaspace* ClassLoaderData::rw_metaspace() { coleenp@4037: assert(_rw_metaspace != NULL, "should already be initialized"); coleenp@4037: return _rw_metaspace; coleenp@4037: } coleenp@4037: coleenp@4037: coleenp@4037: ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() { coleenp@4037: _data = ClassLoaderDataGraph::_head; coleenp@4037: } coleenp@4037: coleenp@4037: ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {} coleenp@4037: coleenp@4037: #ifndef PRODUCT coleenp@4037: // callable from debugger coleenp@4037: extern "C" int print_loader_data_graph() { coleenp@4037: ClassLoaderDataGraph::dump_on(tty); coleenp@4037: return 0; coleenp@4037: } coleenp@4037: coleenp@4037: void ClassLoaderDataGraph::verify() { coleenp@4037: for (ClassLoaderData* data = _head; data != NULL; data = data->next()) { coleenp@4037: data->verify(); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: void ClassLoaderDataGraph::dump_on(outputStream * const out) { coleenp@4037: for (ClassLoaderData* data = _head; data != NULL; data = data->next()) { coleenp@4037: data->dump(out); coleenp@4037: } coleenp@4037: MetaspaceAux::dump(out); coleenp@4037: } coleenp@4037: coleenp@4037: void ClassLoaderData::print_value_on(outputStream* out) const { coleenp@4037: if (class_loader() == NULL) { coleenp@4037: out->print_cr("NULL class_loader"); coleenp@4037: } else { coleenp@4037: out->print("class loader "PTR_FORMAT, this); coleenp@4037: class_loader()->print_value_on(out); coleenp@4037: } coleenp@4037: } coleenp@4037: #endif // PRODUCT