coleenp@4037: /* coleenp@4490: * Copyright (c) 2012, 2013, 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@4490: #include "classfile/metadataOnStackMark.hpp" coleenp@4037: #include "classfile/systemDictionary.hpp" coleenp@4037: #include "code/codeCache.hpp" mgerdin@5013: #include "memory/gcLocker.hpp" coleenp@4037: #include "memory/metadataFactory.hpp" coleenp@4037: #include "memory/metaspaceShared.hpp" coleenp@4490: #include "memory/oopFactory.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: sla@5237: #if INCLUDE_TRACE sla@5237: #include "trace/tracing.hpp" sla@5237: #endif sla@5237: sla@5237: coleenp@4037: ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL; coleenp@4037: mgerdin@5016: ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) : coleenp@4345: _class_loader(h_class_loader()), coleenp@4345: _is_anonymous(is_anonymous), _keep_alive(is_anonymous), // initially coleenp@4345: _metaspace(NULL), _unloading(false), _klasses(NULL), coleenp@4304: _claimed(0), _jmethod_ids(NULL), _handles(NULL), _deallocate_list(NULL), mgerdin@5016: _next(NULL), _dependencies(dependencies), coleenp@4037: _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) { coleenp@4037: // empty coleenp@4037: } coleenp@4037: coleenp@4304: void ClassLoaderData::init_dependencies(TRAPS) { mgerdin@5016: assert(!Universe::is_fully_initialized(), "should only be called when initializing"); mgerdin@5016: assert(is_the_null_class_loader_data(), "should only call this for the null class loader"); mgerdin@4903: _dependencies.init(CHECK); mgerdin@4903: } mgerdin@4903: mgerdin@4903: void ClassLoaderData::Dependencies::init(TRAPS) { coleenp@4304: // Create empty dependencies array to add to. CMS requires this to be coleenp@4304: // an oop so that it can track additions via card marks. We think. mgerdin@4903: _list_head = oopFactory::new_objectArray(2, CHECK); coleenp@4304: } coleenp@4304: 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); mgerdin@4903: _dependencies.oops_do(f); coleenp@4037: _handles->oops_do(f); coleenp@4037: if (klass_closure != NULL) { coleenp@4037: classes_do(klass_closure); coleenp@4037: } coleenp@4037: } coleenp@4037: mgerdin@4903: void ClassLoaderData::Dependencies::oops_do(OopClosure* f) { mgerdin@4903: f->do_oop((oop*)&_list_head); mgerdin@4903: } mgerdin@4903: 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@4751: assert(k != k->next_link(), "no loops!"); coleenp@4037: } coleenp@4037: } coleenp@4037: sla@5237: void ClassLoaderData::classes_do(void f(Klass * const)) { sla@5237: for (Klass* k = _klasses; k != NULL; k = k->next_link()) { sla@5237: f(k); sla@5237: } sla@5237: } sla@5237: 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@4751: assert(k != k->next_link(), "no loops!"); 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@4304: // Dependency to the null class loader data doesn't need to be recorded coleenp@4304: // because the null class loader data never goes away. coleenp@4304: if (to_cld->is_the_null_class_loader_data()) { coleenp@4037: return; coleenp@4037: } coleenp@4037: coleenp@4304: oop to; coleenp@4304: if (to_cld->is_anonymous()) { coleenp@4304: // Anonymous class dependencies are through the mirror. coleenp@4304: to = k->java_mirror(); coleenp@4304: } else { coleenp@4304: to = to_cld->class_loader(); coleenp@4037: coleenp@4304: // If from_cld is anonymous, even if it's class_loader is a parent of 'to' coleenp@4304: // we still have to add it. The class_loader won't keep from_cld alive. coleenp@4304: if (!from_cld->is_anonymous()) { coleenp@4304: // Check that this dependency isn't from the same or parent class_loader coleenp@4304: oop from = from_cld->class_loader(); coleenp@4304: coleenp@4304: oop curr = from; coleenp@4304: while (curr != NULL) { coleenp@4304: if (curr == to) { coleenp@4304: return; // this class loader is in the parent list, no need to add it. coleenp@4304: } coleenp@4304: curr = java_lang_ClassLoader::parent(curr); coleenp@4304: } coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4037: // It's a dependency we won't find through GC, add it. This is relatively rare coleenp@4304: // Must handle over GC point. coleenp@4304: Handle dependency(THREAD, to); mgerdin@4903: from_cld->_dependencies.add(dependency, CHECK); coleenp@4037: } coleenp@4037: coleenp@4037: mgerdin@4903: void ClassLoaderData::Dependencies::add(Handle dependency, TRAPS) { coleenp@4304: // Check first if this dependency is already in the list. coleenp@4304: // Save a pointer to the last to add to under the lock. mgerdin@4903: objArrayOop ok = _list_head; coleenp@4304: objArrayOop last = NULL; coleenp@4037: while (ok != NULL) { coleenp@4304: last = ok; coleenp@4304: if (ok->obj_at(0) == dependency()) { coleenp@4304: // Don't need to add it coleenp@4304: return; coleenp@4037: } coleenp@4037: ok = (objArrayOop)ok->obj_at(1); coleenp@4037: } coleenp@4304: stefank@4353: // Must handle over GC points stefank@4353: assert (last != NULL, "dependencies should be initialized"); stefank@4353: objArrayHandle last_handle(THREAD, last); stefank@4353: coleenp@4304: // Create a new dependency node with fields for (class_loader or mirror, next) coleenp@4304: objArrayOop deps = oopFactory::new_objectArray(2, CHECK); coleenp@4304: deps->obj_at_put(0, dependency()); coleenp@4304: stefank@4353: // Must handle over GC points coleenp@4304: objArrayHandle new_dependency(THREAD, deps); coleenp@4304: coleenp@4304: // Add the dependency under lock mgerdin@4903: locked_add(last_handle, new_dependency, THREAD); coleenp@4037: } coleenp@4037: mgerdin@4903: void ClassLoaderData::Dependencies::locked_add(objArrayHandle last_handle, mgerdin@4903: objArrayHandle new_dependency, mgerdin@4903: Thread* THREAD) { coleenp@4304: coleenp@4304: // Have to lock and put the new dependency on the end of the dependency coleenp@4304: // array so the card mark for CMS sees that this dependency is new. coleenp@4304: // Can probably do this lock free with some effort. mgerdin@4903: ObjectLocker ol(Handle(THREAD, _list_head), THREAD); coleenp@4304: coleenp@4304: oop loader_or_mirror = new_dependency->obj_at(0); coleenp@4304: coleenp@4304: // Since the dependencies are only added, add to the end. coleenp@4304: objArrayOop end = last_handle(); coleenp@4304: objArrayOop last = NULL; coleenp@4304: while (end != NULL) { coleenp@4304: last = end; coleenp@4304: // check again if another thread added it to the end. coleenp@4304: if (end->obj_at(0) == loader_or_mirror) { coleenp@4304: // Don't need to add it coleenp@4304: return; coleenp@4304: } coleenp@4304: end = (objArrayOop)end->obj_at(1); coleenp@4037: } coleenp@4304: assert (last != NULL, "dependencies should be initialized"); coleenp@4304: // fill in the first element with the oop in new_dependency. coleenp@4304: if (last->obj_at(0) == NULL) { coleenp@4304: last->obj_at_put(0, new_dependency->obj_at(0)); coleenp@4304: } else { coleenp@4304: last->obj_at_put(1, new_dependency()); 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@4304: if (TraceClassLoaderData && Verbose && 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(), hseigel@5784: (void *)k->class_loader(), coleenp@4304: loader_name()); 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@4751: assert(k != k->next_link(), "no loops!"); coleenp@4037: } coleenp@4037: ShouldNotReachHere(); // should have found this class!! coleenp@4037: } coleenp@4037: coleenp@4304: void ClassLoaderData::unload() { coleenp@4304: _unloading = true; coleenp@4304: coleenp@4981: // Tell serviceability tools these classes are unloading coleenp@4981: classes_do(InstanceKlass::notify_unload_class); coleenp@4981: coleenp@4304: if (TraceClassLoaderData) { coleenp@4304: ResourceMark rm; coleenp@4304: tty->print("[ClassLoaderData: unload loader data "PTR_FORMAT, this); hseigel@5784: tty->print(" for instance "PTR_FORMAT" of %s", (void *)class_loader(), coleenp@4304: loader_name()); coleenp@4304: if (is_anonymous()) { coleenp@4304: tty->print(" for anonymous class "PTR_FORMAT " ", _klasses); coleenp@4304: } coleenp@4304: tty->print_cr("]"); coleenp@4304: } coleenp@4304: } coleenp@4304: coleenp@4304: bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const { coleenp@4304: bool alive = coleenp@4304: is_anonymous() ? coleenp@4304: is_alive_closure->do_object_b(_klasses->java_mirror()) : coleenp@4304: class_loader() == NULL || is_alive_closure->do_object_b(class_loader()); coleenp@4304: assert(!alive || claimed(), "must be claimed"); coleenp@4304: return alive; coleenp@4304: } coleenp@4304: coleenp@4304: coleenp@4037: ClassLoaderData::~ClassLoaderData() { coleenp@4981: // Release C heap structures for all the classes. coleenp@4981: classes_do(InstanceKlass::release_C_heap_structures); coleenp@4981: 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: twisti@4866: /** twisti@4866: * Returns true if this class loader data is for the extension class loader. twisti@4866: */ twisti@4866: bool ClassLoaderData::is_ext_class_loader_data() const { twisti@4866: return SystemDictionary::is_ext_class_loader(class_loader()); twisti@4866: } twisti@4866: coleenp@4037: Metaspace* ClassLoaderData::metaspace_non_null() { jmasa@4457: assert(!DumpSharedSpaces, "wrong metaspace!"); 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@4304: if (this == the_null_class_loader_data()) { coleenp@4304: assert (class_loader() == NULL, "Must be"); jmasa@4382: set_metaspace(new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType)); jmasa@4382: } else if (is_anonymous()) { jmasa@4382: if (TraceClassLoaderData && Verbose && class_loader() != NULL) { jmasa@4382: tty->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name()); jmasa@4382: } jmasa@4382: set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType)); jmasa@4382: } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) { jmasa@4382: if (TraceClassLoaderData && Verbose && class_loader() != NULL) { jmasa@4382: tty->print_cr("is_reflection: %s", class_loader()->klass()->internal_name()); jmasa@4382: } jmasa@4382: set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType)); coleenp@4037: } else { jmasa@4382: set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType)); 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@4304: // These anonymous class loaders are to contain classes used for JSR292 coleenp@4304: ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(oop loader, TRAPS) { coleenp@4304: // Add a new class loader data to the graph. mgerdin@5013: return ClassLoaderDataGraph::add(loader, true, CHECK_NULL); coleenp@4037: } coleenp@4037: coleenp@4304: const char* ClassLoaderData::loader_name() { coleenp@4304: // Handles null class loader coleenp@4304: return SystemDictionary::loader_name(class_loader()); coleenp@4304: } coleenp@4304: coleenp@4304: #ifndef PRODUCT 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 {", hseigel@5784: this, (void *)class_loader(), coleenp@4304: class_loader() != NULL ? class_loader()->klass() : NULL, loader_name()); 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@4751: assert(k != k->next_link(), "no loops!"); 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@4304: guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same"); coleenp@4304: guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "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@4751: assert(k != k->next_link(), "no loops!"); coleenp@4037: } coleenp@4037: } coleenp@4037: coleenp@4304: 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: // 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 mgerdin@5013: ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous, TRAPS) { mgerdin@5016: // We need to allocate all the oops for the ClassLoaderData before allocating the mgerdin@5016: // actual ClassLoaderData object. mgerdin@5016: ClassLoaderData::Dependencies dependencies(CHECK_NULL); coleenp@4345: mgerdin@5016: No_Safepoint_Verifier no_safepoints; // we mustn't GC until we've installed the mgerdin@5016: // ClassLoaderData in the graph since the CLD mgerdin@5016: // contains unhandled oops coleenp@4037: mgerdin@5016: ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous, dependencies); mgerdin@5016: mgerdin@4903: mgerdin@5013: if (!is_anonymous) { mgerdin@5013: ClassLoaderData** cld_addr = java_lang_ClassLoader::loader_data_addr(loader()); coleenp@4304: // First, Atomically set it coleenp@4304: ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL); coleenp@4304: if (old != NULL) { coleenp@4304: delete cld; coleenp@4304: // Returns the data. coleenp@4304: return old; coleenp@4304: } 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 mgerdin@5013: ClassLoaderData** list_head = &_head; mgerdin@5013: ClassLoaderData* next = _head; mgerdin@5013: 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@4304: ResourceMark rm; coleenp@4037: tty->print("[ClassLoaderData: "); coleenp@4037: tty->print("create class loader data "PTR_FORMAT, cld); hseigel@5784: tty->print(" for instance "PTR_FORMAT" of %s", (void *)cld->class_loader(), coleenp@4304: cld->loader_name()); coleenp@4037: tty->print_cr("]"); coleenp@4037: } coleenp@4037: return cld; coleenp@4037: } coleenp@4037: next = exchanged; coleenp@4037: } while (true); coleenp@4304: 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@4304: void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) { coleenp@4304: for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { coleenp@4304: if (cld->keep_alive()) { coleenp@4304: cld->oops_do(f, klass_closure, must_claim); coleenp@4304: } coleenp@4304: } coleenp@4304: } coleenp@4304: 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@4304: // keep any special CLDs alive. coleenp@4304: ClassLoaderDataGraph::keep_alive_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: sla@5237: void ClassLoaderDataGraph::classes_do(void f(Klass* const)) { sla@5237: for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) { sla@5237: cld->classes_do(f); sla@5237: } sla@5237: } sla@5237: sla@5237: void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) { sla@5237: assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); sla@5237: for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) { sla@5237: cld->classes_do(f); sla@5237: } sla@5237: } sla@5237: 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@4304: coleenp@4037: // Move class loader data from main list to the unloaded list for unloading coleenp@4037: // and deallocation later. coleenp@4304: bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive_closure) { 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@4304: if (data->keep_alive() || data->is_alive(is_alive_closure)) { 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@4304: dead->unload(); coleenp@4037: data = data->next(); coleenp@4037: // Remove from loader list. jmasa@5015: // This class loader data will no longer be found jmasa@5015: // in the ClassLoaderDataGraph. 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: } sla@5237: sla@5237: if (seen_dead_loader) { sla@5237: post_class_unload_events(); sla@5237: } sla@5237: 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: } jmasa@5007: Metaspace::purge(); coleenp@4037: } coleenp@4037: sla@5237: void ClassLoaderDataGraph::post_class_unload_events(void) { sla@5237: #if INCLUDE_TRACE sla@5237: assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!"); sla@5237: if (Tracing::enabled()) { sla@5237: if (Tracing::is_event_enabled(TraceClassUnloadEvent)) { sla@5237: assert(_unloading != NULL, "need class loader data unload list!"); sla@5237: _class_unload_time = Tracing::time(); sla@5237: classes_unloading_do(&class_unload_event); sla@5237: } sla@5237: Tracing::on_unloading_classes(); sla@5237: } sla@5237: #endif sla@5237: } sla@5237: 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); jmasa@4382: _ro_metaspace = new Metaspace(_metaspace_lock, Metaspace::ROMetaspaceType); jmasa@4382: _rw_metaspace = new Metaspace(_metaspace_lock, Metaspace::ReadWriteMetaspaceType); 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: } acorn@4497: #endif // PRODUCT coleenp@4037: coleenp@4037: void ClassLoaderData::print_value_on(outputStream* out) const { coleenp@4037: if (class_loader() == NULL) { acorn@4497: out->print("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: } sla@5237: sla@5237: #if INCLUDE_TRACE sla@5237: sla@5237: TracingTime ClassLoaderDataGraph::_class_unload_time; sla@5237: sla@5237: void ClassLoaderDataGraph::class_unload_event(Klass* const k) { sla@5237: sla@5237: // post class unload event sla@5237: EventClassUnload event(UNTIMED); sla@5237: event.set_endtime(_class_unload_time); sla@5237: event.set_unloadedClass(k); sla@5237: oop defining_class_loader = k->class_loader(); sla@5237: event.set_definingClassLoader(defining_class_loader != NULL ? sla@5237: defining_class_loader->klass() : (Klass*)NULL); sla@5237: event.commit(); sla@5237: } sla@5237: sla@5237: #endif /* INCLUDE_TRACE */