Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/classfile/classLoaderData.cpp
32285 views
/*1* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324// A ClassLoaderData identifies the full set of class types that a class25// loader's name resolution strategy produces for a given configuration of the26// class loader.27// Class types in the ClassLoaderData may be defined by from class file binaries28// provided by the class loader, or from other class loader it interacts with29// according to its name resolution strategy.30//31// Class loaders that implement a deterministic name resolution strategy32// (including with respect to their delegation behavior), such as the boot, the33// extension, and the system loaders of the JDK's built-in class loader34// hierarchy, always produce the same linkset for a given configuration.35//36// ClassLoaderData carries information related to a linkset (e.g.,37// metaspace holding its klass definitions).38// The System Dictionary and related data structures (e.g., placeholder table,39// loader constraints table) as well as the runtime representation of classes40// only reference ClassLoaderData.41//42// Instances of java.lang.ClassLoader holds a pointer to a ClassLoaderData that43// that represent the loader's "linking domain" in the JVM.44//45// The bootstrap loader (represented by NULL) also has a ClassLoaderData,46// the singleton class the_null_class_loader_data().4748#include "precompiled.hpp"49#include "classfile/classLoaderData.hpp"50#include "classfile/classLoaderData.inline.hpp"51#include "classfile/javaClasses.hpp"52#include "classfile/metadataOnStackMark.hpp"53#include "classfile/systemDictionary.hpp"54#include "code/codeCache.hpp"55#include "memory/gcLocker.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/macros.hpp"65#include "utilities/ostream.hpp"6667ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;6869ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) :70_class_loader(h_class_loader()),71_is_anonymous(is_anonymous),72// An anonymous class loader data doesn't have anything to keep73// it from being unloaded during parsing of the anonymous class.74// The null-class-loader should always be kept alive.75_keep_alive(is_anonymous || h_class_loader.is_null()),76_metaspace(NULL), _unloading(false), _klasses(NULL),77_claimed(0), _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),78_next(NULL), _dependencies(dependencies),79_metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {8081JFR_ONLY(INIT_ID(this);)82}8384void ClassLoaderData::init_dependencies(TRAPS) {85assert(!Universe::is_fully_initialized(), "should only be called when initializing");86assert(is_the_null_class_loader_data(), "should only call this for the null class loader");87_dependencies.init(CHECK);88}8990void ClassLoaderData::Dependencies::init(TRAPS) {91// Create empty dependencies array to add to. CMS requires this to be92// an oop so that it can track additions via card marks. We think.93_list_head = oopFactory::new_objectArray(2, CHECK);94}9596ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() {97Chunk* c = _head;98while (c != NULL) {99Chunk* next = c->_next;100delete c;101c = next;102}103}104105oop* ClassLoaderData::ChunkedHandleList::add(oop o) {106if (_head == NULL || _head->_size == Chunk::CAPACITY) {107Chunk* next = new Chunk(_head);108OrderAccess::release_store_ptr(&_head, next);109}110oop* handle = &_head->_data[_head->_size];111*handle = o;112OrderAccess::release_store(&_head->_size, _head->_size + 1);113return handle;114}115116inline void ClassLoaderData::ChunkedHandleList::oops_do_chunk(OopClosure* f, Chunk* c, const juint size) {117for (juint i = 0; i < size; i++) {118if (c->_data[i] != NULL) {119f->do_oop(&c->_data[i]);120}121}122}123124void ClassLoaderData::ChunkedHandleList::oops_do(OopClosure* f) {125Chunk* head = (Chunk*) OrderAccess::load_ptr_acquire(&_head);126if (head != NULL) {127// Must be careful when reading size of head128oops_do_chunk(f, head, OrderAccess::load_acquire(&head->_size));129for (Chunk* c = head->_next; c != NULL; c = c->_next) {130oops_do_chunk(f, c, c->_size);131}132}133}134135bool ClassLoaderData::claim() {136if (_claimed == 1) {137return false;138}139140return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0;141}142143void ClassLoaderData::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {144if (must_claim && !claim()) {145return;146}147148f->do_oop(&_class_loader);149_dependencies.oops_do(f);150_handles.oops_do(f);151if (klass_closure != NULL) {152classes_do(klass_closure);153}154}155156void ClassLoaderData::Dependencies::oops_do(OopClosure* f) {157f->do_oop((oop*)&_list_head);158}159160void ClassLoaderData::classes_do(KlassClosure* klass_closure) {161for (Klass* k = _klasses; k != NULL; k = k->next_link()) {162klass_closure->do_klass(k);163assert(k != k->next_link(), "no loops!");164}165}166167void ClassLoaderData::classes_do(void f(Klass * const)) {168for (Klass* k = _klasses; k != NULL; k = k->next_link()) {169f(k);170}171}172173void ClassLoaderData::loaded_classes_do(KlassClosure* klass_closure) {174// Lock to avoid classes being modified/added/removed during iteration175MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);176for (Klass* k = _klasses; k != NULL; k = k->next_link()) {177// Do not filter ArrayKlass oops here...178if (k->oop_is_array() || (k->oop_is_instance() && InstanceKlass::cast(k)->is_loaded())) {179klass_closure->do_klass(k);180}181}182}183184void ClassLoaderData::classes_do(void f(InstanceKlass*)) {185for (Klass* k = _klasses; k != NULL; k = k->next_link()) {186if (k->oop_is_instance()) {187f(InstanceKlass::cast(k));188}189assert(k != k->next_link(), "no loops!");190}191}192193void ClassLoaderData::record_dependency(Klass* k, TRAPS) {194ClassLoaderData * const from_cld = this;195ClassLoaderData * const to_cld = k->class_loader_data();196197// Dependency to the null class loader data doesn't need to be recorded198// because the null class loader data never goes away.199if (to_cld->is_the_null_class_loader_data()) {200return;201}202203oop to;204if (to_cld->is_anonymous()) {205// Anonymous class dependencies are through the mirror.206to = k->java_mirror();207} else {208to = to_cld->class_loader();209210// If from_cld is anonymous, even if it's class_loader is a parent of 'to'211// we still have to add it. The class_loader won't keep from_cld alive.212if (!from_cld->is_anonymous()) {213// Check that this dependency isn't from the same or parent class_loader214oop from = from_cld->class_loader();215216oop curr = from;217while (curr != NULL) {218if (curr == to) {219return; // this class loader is in the parent list, no need to add it.220}221curr = java_lang_ClassLoader::parent(curr);222}223}224}225226// It's a dependency we won't find through GC, add it. This is relatively rare227// Must handle over GC point.228Handle dependency(THREAD, to);229from_cld->_dependencies.add(dependency, CHECK);230}231232233void ClassLoaderData::Dependencies::add(Handle dependency, TRAPS) {234// Check first if this dependency is already in the list.235// Save a pointer to the last to add to under the lock.236objArrayOop ok = _list_head;237objArrayOop last = NULL;238while (ok != NULL) {239last = ok;240if (ok->obj_at(0) == dependency()) {241// Don't need to add it242return;243}244ok = (objArrayOop)ok->obj_at(1);245}246247// Must handle over GC points248assert (last != NULL, "dependencies should be initialized");249objArrayHandle last_handle(THREAD, last);250251// Create a new dependency node with fields for (class_loader or mirror, next)252objArrayOop deps = oopFactory::new_objectArray(2, CHECK);253deps->obj_at_put(0, dependency());254255// Must handle over GC points256objArrayHandle new_dependency(THREAD, deps);257258// Add the dependency under lock259locked_add(last_handle, new_dependency, THREAD);260}261262void ClassLoaderData::Dependencies::locked_add(objArrayHandle last_handle,263objArrayHandle new_dependency,264Thread* THREAD) {265266// Have to lock and put the new dependency on the end of the dependency267// array so the card mark for CMS sees that this dependency is new.268// Can probably do this lock free with some effort.269ObjectLocker ol(Handle(THREAD, _list_head), THREAD);270271oop loader_or_mirror = new_dependency->obj_at(0);272273// Since the dependencies are only added, add to the end.274objArrayOop end = last_handle();275objArrayOop last = NULL;276while (end != NULL) {277last = end;278// check again if another thread added it to the end.279if (end->obj_at(0) == loader_or_mirror) {280// Don't need to add it281return;282}283end = (objArrayOop)end->obj_at(1);284}285assert (last != NULL, "dependencies should be initialized");286// fill in the first element with the oop in new_dependency.287if (last->obj_at(0) == NULL) {288last->obj_at_put(0, new_dependency->obj_at(0));289} else {290last->obj_at_put(1, new_dependency());291}292}293294void ClassLoaderDataGraph::clear_claimed_marks() {295for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {296cld->clear_claimed();297}298}299300void ClassLoaderData::add_class(Klass* k) {301MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);302Klass* old_value = _klasses;303k->set_next_link(old_value);304// link the new item into the list305_klasses = k;306307if (TraceClassLoaderData && Verbose && k->class_loader_data() != NULL) {308ResourceMark rm;309tty->print_cr("[TraceClassLoaderData] Adding k: " PTR_FORMAT " %s to CLD: "310PTR_FORMAT " loader: " PTR_FORMAT " %s",311p2i(k),312k->external_name(),313p2i(k->class_loader_data()),314p2i((void *)k->class_loader()),315loader_name());316}317}318319// This is called by InstanceKlass::deallocate_contents() to remove the320// scratch_class for redefine classes. We need a lock because there it may not321// be called at a safepoint if there's an error.322void ClassLoaderData::remove_class(Klass* scratch_class) {323MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);324Klass* prev = NULL;325for (Klass* k = _klasses; k != NULL; k = k->next_link()) {326if (k == scratch_class) {327if (prev == NULL) {328_klasses = k->next_link();329} else {330Klass* next = k->next_link();331prev->set_next_link(next);332}333return;334}335prev = k;336assert(k != k->next_link(), "no loops!");337}338ShouldNotReachHere(); // should have found this class!!339}340341void ClassLoaderData::unload() {342_unloading = true;343344// Tell serviceability tools these classes are unloading345classes_do(InstanceKlass::notify_unload_class);346347if (TraceClassLoaderData) {348ResourceMark rm;349tty->print("[ClassLoaderData: unload loader data " INTPTR_FORMAT, p2i(this));350tty->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)class_loader()),351loader_name());352if (is_anonymous()) {353tty->print(" for anonymous class " INTPTR_FORMAT " ", p2i(_klasses));354}355tty->print_cr("]");356}357}358359oop ClassLoaderData::keep_alive_object() const {360assert(!keep_alive(), "Don't use with CLDs that are artificially kept alive");361return is_anonymous() ? _klasses->java_mirror() : class_loader();362}363364bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {365bool alive = keep_alive() // null class loader and incomplete anonymous klasses.366|| is_alive_closure->do_object_b(keep_alive_object());367368return alive;369}370371372ClassLoaderData::~ClassLoaderData() {373// Release C heap structures for all the classes.374classes_do(InstanceKlass::release_C_heap_structures);375376Metaspace *m = _metaspace;377if (m != NULL) {378_metaspace = NULL;379// release the metaspace380delete m;381}382383// Clear all the JNI handles for methods384// These aren't deallocated and are going to look like a leak, but that's385// needed because we can't really get rid of jmethodIDs because we don't386// know when native code is going to stop using them. The spec says that387// they're "invalid" but existing programs likely rely on their being388// NULL after class unloading.389if (_jmethod_ids != NULL) {390Method::clear_jmethod_ids(this);391}392// Delete lock393delete _metaspace_lock;394395// Delete free list396if (_deallocate_list != NULL) {397delete _deallocate_list;398}399}400401/**402* Returns true if this class loader data is for the extension class loader.403*/404bool ClassLoaderData::is_ext_class_loader_data() const {405return SystemDictionary::is_ext_class_loader(class_loader());406}407408Metaspace* ClassLoaderData::metaspace_non_null() {409assert(!DumpSharedSpaces, "wrong metaspace!");410// If the metaspace has not been allocated, create a new one. Might want411// to create smaller arena for Reflection class loaders also.412// The reason for the delayed allocation is because some class loaders are413// simply for delegating with no metadata of their own.414if (_metaspace == NULL) {415MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);416// Check again if metaspace has been allocated while we were getting this lock.417if (_metaspace != NULL) {418return _metaspace;419}420if (this == the_null_class_loader_data()) {421assert (class_loader() == NULL, "Must be");422set_metaspace(new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType));423} else if (is_anonymous()) {424if (TraceClassLoaderData && Verbose && class_loader() != NULL) {425tty->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name());426}427set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType));428} else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {429if (TraceClassLoaderData && Verbose && class_loader() != NULL) {430tty->print_cr("is_reflection: %s", class_loader()->klass()->internal_name());431}432set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType));433} else {434set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType));435}436}437return _metaspace;438}439440jobject ClassLoaderData::add_handle(Handle h) {441MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);442return (jobject) _handles.add(h());443}444445// Add this metadata pointer to be freed when it's safe. This is only during446// class unloading because Handles might point to this metadata field.447void ClassLoaderData::add_to_deallocate_list(Metadata* m) {448// Metadata in shared region isn't deleted.449if (!m->is_shared()) {450MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);451if (_deallocate_list == NULL) {452_deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);453}454_deallocate_list->append_if_missing(m);455}456}457458// Deallocate free metadata on the free list. How useful the PermGen was!459void ClassLoaderData::free_deallocate_list() {460// Don't need lock, at safepoint461assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");462if (_deallocate_list == NULL) {463return;464}465// Go backwards because this removes entries that are freed.466for (int i = _deallocate_list->length() - 1; i >= 0; i--) {467Metadata* m = _deallocate_list->at(i);468if (!m->on_stack()) {469_deallocate_list->remove_at(i);470// There are only three types of metadata that we deallocate directly.471// Cast them so they can be used by the template function.472if (m->is_method()) {473MetadataFactory::free_metadata(this, (Method*)m);474} else if (m->is_constantPool()) {475MetadataFactory::free_metadata(this, (ConstantPool*)m);476} else if (m->is_klass()) {477MetadataFactory::free_metadata(this, (InstanceKlass*)m);478} else {479ShouldNotReachHere();480}481}482}483}484485// These anonymous class loaders are to contain classes used for JSR292486ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(oop loader, TRAPS) {487// Add a new class loader data to the graph.488return ClassLoaderDataGraph::add(loader, true, THREAD);489}490491const char* ClassLoaderData::loader_name() {492// Handles null class loader493return SystemDictionary::loader_name(class_loader());494}495496#ifndef PRODUCT497// Define to dump klasses498#undef CLD_DUMP_KLASSES499500void ClassLoaderData::dump(outputStream * const out) {501ResourceMark rm;502out->print("ClassLoaderData CLD: " PTR_FORMAT ", loader: " PTR_FORMAT ", loader_klass: " PTR_FORMAT " %s {",503p2i(this), p2i((void *)class_loader()),504p2i(class_loader() != NULL ? class_loader()->klass() : NULL), loader_name());505if (claimed()) out->print(" claimed ");506if (is_unloading()) out->print(" unloading ");507out->cr();508if (metaspace_or_null() != NULL) {509out->print_cr("metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null()));510metaspace_or_null()->dump(out);511} else {512out->print_cr("metaspace: NULL");513}514515#ifdef CLD_DUMP_KLASSES516if (Verbose) {517ResourceMark rm;518Klass* k = _klasses;519while (k != NULL) {520out->print_cr("klass " PTR_FORMAT ", %s, CT: %d, MUT: %d", k, k->name()->as_C_string(),521k->has_modified_oops(), k->has_accumulated_modified_oops());522assert(k != k->next_link(), "no loops!");523k = k->next_link();524}525}526#endif // CLD_DUMP_KLASSES527#undef CLD_DUMP_KLASSES528if (_jmethod_ids != NULL) {529Method::print_jmethod_ids(this, out);530}531out->print_cr("}");532}533#endif // PRODUCT534535void ClassLoaderData::verify() {536oop cl = class_loader();537538guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same");539guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be");540541// Verify the integrity of the allocated space.542if (metaspace_or_null() != NULL) {543metaspace_or_null()->verify();544}545546for (Klass* k = _klasses; k != NULL; k = k->next_link()) {547guarantee(k->class_loader_data() == this, "Must be the same");548k->verify();549assert(k != k->next_link(), "no loops!");550}551}552553bool ClassLoaderData::contains_klass(Klass* klass) {554for (Klass* k = _klasses; k != NULL; k = k->next_link()) {555if (k == klass) return true;556}557return false;558}559560561// GC root of class loader data created.562ClassLoaderData* ClassLoaderDataGraph::_head = NULL;563ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;564ClassLoaderData* ClassLoaderDataGraph::_saved_unloading = NULL;565ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;566567bool ClassLoaderDataGraph::_should_purge = false;568569// Add a new class loader data node to the list. Assign the newly created570// ClassLoaderData into the java/lang/ClassLoader object as a hidden field571ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous, TRAPS) {572// We need to allocate all the oops for the ClassLoaderData before allocating the573// actual ClassLoaderData object.574ClassLoaderData::Dependencies dependencies(CHECK_NULL);575576No_Safepoint_Verifier no_safepoints; // we mustn't GC until we've installed the577// ClassLoaderData in the graph since the CLD578// contains unhandled oops579580ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous, dependencies);581582583if (!is_anonymous) {584ClassLoaderData** cld_addr = java_lang_ClassLoader::loader_data_addr(loader());585// First, Atomically set it586ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL);587if (old != NULL) {588delete cld;589// Returns the data.590return old;591}592}593594// We won the race, and therefore the task of adding the data to the list of595// class loader data596ClassLoaderData** list_head = &_head;597ClassLoaderData* next = _head;598599do {600cld->set_next(next);601ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next);602if (exchanged == next) {603if (TraceClassLoaderData) {604ResourceMark rm;605tty->print("[ClassLoaderData: ");606tty->print("create class loader data " INTPTR_FORMAT, p2i(cld));607tty->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)cld->class_loader()),608cld->loader_name());609tty->print_cr("]");610}611return cld;612}613next = exchanged;614} while (true);615616}617618void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {619for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {620cld->oops_do(f, klass_closure, must_claim);621}622}623624void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {625for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {626if (cld->keep_alive()) {627cld->oops_do(f, klass_closure, must_claim);628}629}630}631632void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {633if (ClassUnloading) {634keep_alive_oops_do(f, klass_closure, must_claim);635} else {636oops_do(f, klass_closure, must_claim);637}638}639640void ClassLoaderDataGraph::cld_do(CLDClosure* cl) {641for (ClassLoaderData* cld = _head; cl != NULL && cld != NULL; cld = cld->next()) {642cl->do_cld(cld);643}644}645646void ClassLoaderDataGraph::cld_unloading_do(CLDClosure* cl) {647assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");648// Only walk the head until any clds not purged from prior unloading649// (CMS doesn't purge right away).650for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {651assert(cld->is_unloading(), "invariant");652cl->do_cld(cld);653}654}655656void ClassLoaderDataGraph::roots_cld_do(CLDClosure* strong, CLDClosure* weak) {657for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->_next) {658CLDClosure* closure = cld->keep_alive() ? strong : weak;659if (closure != NULL) {660closure->do_cld(cld);661}662}663}664665void ClassLoaderDataGraph::keep_alive_cld_do(CLDClosure* cl) {666roots_cld_do(cl, NULL);667}668669void ClassLoaderDataGraph::always_strong_cld_do(CLDClosure* cl) {670if (ClassUnloading) {671keep_alive_cld_do(cl);672} else {673cld_do(cl);674}675}676677void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) {678for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {679cld->classes_do(klass_closure);680}681}682683void ClassLoaderDataGraph::classes_do(void f(Klass* const)) {684for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {685cld->classes_do(f);686}687}688689void ClassLoaderDataGraph::loaded_classes_do(KlassClosure* klass_closure) {690for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {691cld->loaded_classes_do(klass_closure);692}693}694695void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {696assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");697// Only walk the head until any clds not purged from prior unloading698// (CMS doesn't purge right away).699for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {700cld->classes_do(f);701}702}703704GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {705assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");706707GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();708709// The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);710ClassLoaderData* curr = _head;711while (curr != _saved_head) {712if (!curr->claimed()) {713array->push(curr);714715if (TraceClassLoaderData) {716tty->print("[ClassLoaderData] found new CLD: ");717curr->print_value_on(tty);718tty->cr();719}720}721722curr = curr->_next;723}724725return array;726}727728bool ClassLoaderDataGraph::unload_list_contains(const void* x) {729assert(SafepointSynchronize::is_at_safepoint(), "only safe to call at safepoint");730for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) {731if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {732return true;733}734}735return false;736}737738#ifndef PRODUCT739bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {740for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {741if (loader_data == data) {742return true;743}744}745746return false;747}748#endif // PRODUCT749750// Move class loader data from main list to the unloaded list for unloading751// and deallocation later.752bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive_closure, bool clean_alive) {753ClassLoaderData* data = _head;754ClassLoaderData* prev = NULL;755bool seen_dead_loader = false;756757// Save previous _unloading pointer for CMS which may add to unloading list before758// purging and we don't want to rewalk the previously unloaded class loader data.759_saved_unloading = _unloading;760761while (data != NULL) {762if (data->is_alive(is_alive_closure)) {763prev = data;764data = data->next();765continue;766}767seen_dead_loader = true;768ClassLoaderData* dead = data;769dead->unload();770data = data->next();771// Remove from loader list.772// This class loader data will no longer be found773// in the ClassLoaderDataGraph.774if (prev != NULL) {775prev->set_next(data);776} else {777assert(dead == _head, "sanity check");778_head = data;779}780dead->set_next(_unloading);781_unloading = dead;782}783784if (clean_alive) {785// Clean previous versions and the deallocate list.786ClassLoaderDataGraph::clean_metaspaces();787}788789return seen_dead_loader;790}791792void ClassLoaderDataGraph::clean_metaspaces() {793// mark metadata seen on the stack and code cache so we can delete unneeded entries.794bool has_redefined_a_class = JvmtiExport::has_redefined_a_class();795MetadataOnStackMark md_on_stack(has_redefined_a_class);796797if (has_redefined_a_class) {798// purge_previous_versions also cleans weak method links. Because799// one method's MDO can reference another method from another800// class loader, we need to first clean weak method links for all801// class loaders here. Below, we can then free redefined methods802// for all class loaders.803for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {804data->classes_do(InstanceKlass::purge_previous_versions);805}806}807808// Need to purge the previous version before deallocating.809free_deallocate_lists();810}811812void ClassLoaderDataGraph::purge() {813assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");814ClassLoaderData* list = _unloading;815_unloading = NULL;816ClassLoaderData* next = list;817while (next != NULL) {818ClassLoaderData* purge_me = next;819next = purge_me->next();820delete purge_me;821}822Metaspace::purge();823}824825void ClassLoaderDataGraph::free_deallocate_lists() {826for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {827// We need to keep this data until InstanceKlass::purge_previous_version has been828// called on all alive classes. See the comment in ClassLoaderDataGraph::clean_metaspaces.829cld->free_deallocate_list();830}831832// In some rare cases items added to the unloading list will not be freed elsewhere.833// To keep it simple, walk the _unloading list also.834for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {835cld->free_deallocate_list();836}837}838839// CDS support840841// Global metaspaces for writing information to the shared archive. When842// application CDS is supported, we may need one per metaspace, so this843// sort of looks like it.844Metaspace* ClassLoaderData::_ro_metaspace = NULL;845Metaspace* ClassLoaderData::_rw_metaspace = NULL;846static bool _shared_metaspaces_initialized = false;847848// Initialize shared metaspaces (change to call from somewhere not lazily)849void ClassLoaderData::initialize_shared_metaspaces() {850assert(DumpSharedSpaces, "only use this for dumping shared spaces");851assert(this == ClassLoaderData::the_null_class_loader_data(),852"only supported for null loader data for now");853assert (!_shared_metaspaces_initialized, "only initialize once");854MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);855_ro_metaspace = new Metaspace(_metaspace_lock, Metaspace::ROMetaspaceType);856_rw_metaspace = new Metaspace(_metaspace_lock, Metaspace::ReadWriteMetaspaceType);857_shared_metaspaces_initialized = true;858}859860Metaspace* ClassLoaderData::ro_metaspace() {861assert(_ro_metaspace != NULL, "should already be initialized");862return _ro_metaspace;863}864865Metaspace* ClassLoaderData::rw_metaspace() {866assert(_rw_metaspace != NULL, "should already be initialized");867return _rw_metaspace;868}869870ClassLoaderDataGraphKlassIteratorAtomic::ClassLoaderDataGraphKlassIteratorAtomic()871: _next_klass(NULL) {872ClassLoaderData* cld = ClassLoaderDataGraph::_head;873Klass* klass = NULL;874875// Find the first klass in the CLDG.876while (cld != NULL) {877klass = cld->_klasses;878if (klass != NULL) {879_next_klass = klass;880return;881}882cld = cld->next();883}884}885886Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass_in_cldg(Klass* klass) {887Klass* next = klass->next_link();888if (next != NULL) {889return next;890}891892// No more klasses in the current CLD. Time to find a new CLD.893ClassLoaderData* cld = klass->class_loader_data();894while (next == NULL) {895cld = cld->next();896if (cld == NULL) {897break;898}899next = cld->_klasses;900}901902return next;903}904905Klass* ClassLoaderDataGraphKlassIteratorAtomic::next_klass() {906Klass* head = _next_klass;907908while (head != NULL) {909Klass* next = next_klass_in_cldg(head);910911Klass* old_head = (Klass*)Atomic::cmpxchg_ptr(next, &_next_klass, head);912913if (old_head == head) {914return head; // Won the CAS.915}916917head = old_head;918}919920// Nothing more for the iterator to hand out.921assert(head == NULL, err_msg("head is " PTR_FORMAT ", expected not null:", p2i(head)));922return NULL;923}924925ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() {926_data = ClassLoaderDataGraph::_head;927}928929ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {}930931#ifndef PRODUCT932// callable from debugger933extern "C" int print_loader_data_graph() {934ClassLoaderDataGraph::dump_on(tty);935return 0;936}937938void ClassLoaderDataGraph::verify() {939for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {940data->verify();941}942}943944void ClassLoaderDataGraph::dump_on(outputStream * const out) {945for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {946data->dump(out);947}948MetaspaceAux::dump(out);949}950#endif // PRODUCT951952void ClassLoaderData::print_value_on(outputStream* out) const {953if (class_loader() == NULL) {954out->print("NULL class_loader");955} else {956out->print("class loader " INTPTR_FORMAT, p2i(this));957class_loader()->print_value_on(out);958}959}960961962