src/share/vm/classfile/classLoaderData.cpp

Thu, 26 Sep 2013 10:25:02 -0400

author
hseigel
date
Thu, 26 Sep 2013 10:25:02 -0400
changeset 5784
190899198332
parent 5237
f2110083203d
child 6024
e64f1fe9756b
permissions
-rw-r--r--

7195622: CheckUnhandledOops has limited usefulness now
Summary: Enable CHECK_UNHANDLED_OOPS in fastdebug builds across all supported platforms.
Reviewed-by: coleenp, hseigel, dholmes, stefank, twisti, ihse, rdurbin
Contributed-by: lois.foltan@oracle.com

     1 /*
     2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 // A ClassLoaderData identifies the full set of class types that a class
    26 // loader's name resolution strategy produces for a given configuration of the
    27 // class loader.
    28 // Class types in the ClassLoaderData may be defined by from class file binaries
    29 // provided by the class loader, or from other class loader it interacts with
    30 // according to its name resolution strategy.
    31 //
    32 // Class loaders that implement a deterministic name resolution strategy
    33 // (including with respect to their delegation behavior), such as the boot, the
    34 // extension, and the system loaders of the JDK's built-in class loader
    35 // hierarchy, always produce the same linkset for a given configuration.
    36 //
    37 // ClassLoaderData carries information related to a linkset (e.g.,
    38 // metaspace holding its klass definitions).
    39 // The System Dictionary and related data structures (e.g., placeholder table,
    40 // loader constraints table) as well as the runtime representation of classes
    41 // only reference ClassLoaderData.
    42 //
    43 // Instances of java.lang.ClassLoader holds a pointer to a ClassLoaderData that
    44 // that represent the loader's "linking domain" in the JVM.
    45 //
    46 // The bootstrap loader (represented by NULL) also has a ClassLoaderData,
    47 // the singleton class the_null_class_loader_data().
    49 #include "precompiled.hpp"
    50 #include "classfile/classLoaderData.hpp"
    51 #include "classfile/classLoaderData.inline.hpp"
    52 #include "classfile/javaClasses.hpp"
    53 #include "classfile/metadataOnStackMark.hpp"
    54 #include "classfile/systemDictionary.hpp"
    55 #include "code/codeCache.hpp"
    56 #include "memory/gcLocker.hpp"
    57 #include "memory/metadataFactory.hpp"
    58 #include "memory/metaspaceShared.hpp"
    59 #include "memory/oopFactory.hpp"
    60 #include "runtime/jniHandles.hpp"
    61 #include "runtime/mutex.hpp"
    62 #include "runtime/safepoint.hpp"
    63 #include "runtime/synchronizer.hpp"
    64 #include "utilities/growableArray.hpp"
    65 #include "utilities/ostream.hpp"
    67 #if INCLUDE_TRACE
    68  #include "trace/tracing.hpp"
    69 #endif
    72 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
    74 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) :
    75   _class_loader(h_class_loader()),
    76   _is_anonymous(is_anonymous), _keep_alive(is_anonymous), // initially
    77   _metaspace(NULL), _unloading(false), _klasses(NULL),
    78   _claimed(0), _jmethod_ids(NULL), _handles(NULL), _deallocate_list(NULL),
    79   _next(NULL), _dependencies(dependencies),
    80   _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true)) {
    81     // empty
    82 }
    84 void ClassLoaderData::init_dependencies(TRAPS) {
    85   assert(!Universe::is_fully_initialized(), "should only be called when initializing");
    86   assert(is_the_null_class_loader_data(), "should only call this for the null class loader");
    87   _dependencies.init(CHECK);
    88 }
    90 void ClassLoaderData::Dependencies::init(TRAPS) {
    91   // Create empty dependencies array to add to. CMS requires this to be
    92   // an oop so that it can track additions via card marks.  We think.
    93   _list_head = oopFactory::new_objectArray(2, CHECK);
    94 }
    96 bool ClassLoaderData::claim() {
    97   if (_claimed == 1) {
    98     return false;
    99   }
   101   return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0;
   102 }
   104 void ClassLoaderData::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
   105   if (must_claim && !claim()) {
   106     return;
   107   }
   109   f->do_oop(&_class_loader);
   110   _dependencies.oops_do(f);
   111   _handles->oops_do(f);
   112   if (klass_closure != NULL) {
   113     classes_do(klass_closure);
   114   }
   115 }
   117 void ClassLoaderData::Dependencies::oops_do(OopClosure* f) {
   118   f->do_oop((oop*)&_list_head);
   119 }
   121 void ClassLoaderData::classes_do(KlassClosure* klass_closure) {
   122   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   123     klass_closure->do_klass(k);
   124     assert(k != k->next_link(), "no loops!");
   125   }
   126 }
   128 void ClassLoaderData::classes_do(void f(Klass * const)) {
   129   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   130     f(k);
   131   }
   132 }
   134 void ClassLoaderData::classes_do(void f(InstanceKlass*)) {
   135   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   136     if (k->oop_is_instance()) {
   137       f(InstanceKlass::cast(k));
   138     }
   139     assert(k != k->next_link(), "no loops!");
   140   }
   141 }
   143 void ClassLoaderData::record_dependency(Klass* k, TRAPS) {
   144   ClassLoaderData * const from_cld = this;
   145   ClassLoaderData * const to_cld = k->class_loader_data();
   147   // Dependency to the null class loader data doesn't need to be recorded
   148   // because the null class loader data never goes away.
   149   if (to_cld->is_the_null_class_loader_data()) {
   150     return;
   151   }
   153   oop to;
   154   if (to_cld->is_anonymous()) {
   155     // Anonymous class dependencies are through the mirror.
   156     to = k->java_mirror();
   157   } else {
   158     to = to_cld->class_loader();
   160     // If from_cld is anonymous, even if it's class_loader is a parent of 'to'
   161     // we still have to add it.  The class_loader won't keep from_cld alive.
   162     if (!from_cld->is_anonymous()) {
   163       // Check that this dependency isn't from the same or parent class_loader
   164       oop from = from_cld->class_loader();
   166       oop curr = from;
   167       while (curr != NULL) {
   168         if (curr == to) {
   169           return; // this class loader is in the parent list, no need to add it.
   170         }
   171         curr = java_lang_ClassLoader::parent(curr);
   172       }
   173     }
   174   }
   176   // It's a dependency we won't find through GC, add it. This is relatively rare
   177   // Must handle over GC point.
   178   Handle dependency(THREAD, to);
   179   from_cld->_dependencies.add(dependency, CHECK);
   180 }
   183 void ClassLoaderData::Dependencies::add(Handle dependency, TRAPS) {
   184   // Check first if this dependency is already in the list.
   185   // Save a pointer to the last to add to under the lock.
   186   objArrayOop ok = _list_head;
   187   objArrayOop last = NULL;
   188   while (ok != NULL) {
   189     last = ok;
   190     if (ok->obj_at(0) == dependency()) {
   191       // Don't need to add it
   192       return;
   193     }
   194     ok = (objArrayOop)ok->obj_at(1);
   195   }
   197   // Must handle over GC points
   198   assert (last != NULL, "dependencies should be initialized");
   199   objArrayHandle last_handle(THREAD, last);
   201   // Create a new dependency node with fields for (class_loader or mirror, next)
   202   objArrayOop deps = oopFactory::new_objectArray(2, CHECK);
   203   deps->obj_at_put(0, dependency());
   205   // Must handle over GC points
   206   objArrayHandle new_dependency(THREAD, deps);
   208   // Add the dependency under lock
   209   locked_add(last_handle, new_dependency, THREAD);
   210 }
   212 void ClassLoaderData::Dependencies::locked_add(objArrayHandle last_handle,
   213                                                objArrayHandle new_dependency,
   214                                                Thread* THREAD) {
   216   // Have to lock and put the new dependency on the end of the dependency
   217   // array so the card mark for CMS sees that this dependency is new.
   218   // Can probably do this lock free with some effort.
   219   ObjectLocker ol(Handle(THREAD, _list_head), THREAD);
   221   oop loader_or_mirror = new_dependency->obj_at(0);
   223   // Since the dependencies are only added, add to the end.
   224   objArrayOop end = last_handle();
   225   objArrayOop last = NULL;
   226   while (end != NULL) {
   227     last = end;
   228     // check again if another thread added it to the end.
   229     if (end->obj_at(0) == loader_or_mirror) {
   230       // Don't need to add it
   231       return;
   232     }
   233     end = (objArrayOop)end->obj_at(1);
   234   }
   235   assert (last != NULL, "dependencies should be initialized");
   236   // fill in the first element with the oop in new_dependency.
   237   if (last->obj_at(0) == NULL) {
   238     last->obj_at_put(0, new_dependency->obj_at(0));
   239   } else {
   240     last->obj_at_put(1, new_dependency());
   241   }
   242 }
   244 void ClassLoaderDataGraph::clear_claimed_marks() {
   245   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   246     cld->clear_claimed();
   247   }
   248 }
   250 void ClassLoaderData::add_class(Klass* k) {
   251   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   252   Klass* old_value = _klasses;
   253   k->set_next_link(old_value);
   254   // link the new item into the list
   255   _klasses = k;
   257   if (TraceClassLoaderData && Verbose && k->class_loader_data() != NULL) {
   258     ResourceMark rm;
   259     tty->print_cr("[TraceClassLoaderData] Adding k: " PTR_FORMAT " %s to CLD: "
   260                   PTR_FORMAT " loader: " PTR_FORMAT " %s",
   261                   k,
   262                   k->external_name(),
   263                   k->class_loader_data(),
   264                   (void *)k->class_loader(),
   265                   loader_name());
   266   }
   267 }
   269 // This is called by InstanceKlass::deallocate_contents() to remove the
   270 // scratch_class for redefine classes.  We need a lock because there it may not
   271 // be called at a safepoint if there's an error.
   272 void ClassLoaderData::remove_class(Klass* scratch_class) {
   273   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   274   Klass* prev = NULL;
   275   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   276     if (k == scratch_class) {
   277       if (prev == NULL) {
   278         _klasses = k->next_link();
   279       } else {
   280         Klass* next = k->next_link();
   281         prev->set_next_link(next);
   282       }
   283       return;
   284     }
   285     prev = k;
   286     assert(k != k->next_link(), "no loops!");
   287   }
   288   ShouldNotReachHere();   // should have found this class!!
   289 }
   291 void ClassLoaderData::unload() {
   292   _unloading = true;
   294   // Tell serviceability tools these classes are unloading
   295   classes_do(InstanceKlass::notify_unload_class);
   297   if (TraceClassLoaderData) {
   298     ResourceMark rm;
   299     tty->print("[ClassLoaderData: unload loader data "PTR_FORMAT, this);
   300     tty->print(" for instance "PTR_FORMAT" of %s", (void *)class_loader(),
   301                loader_name());
   302     if (is_anonymous()) {
   303       tty->print(" for anonymous class  "PTR_FORMAT " ", _klasses);
   304     }
   305     tty->print_cr("]");
   306   }
   307 }
   309 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {
   310   bool alive =
   311     is_anonymous() ?
   312        is_alive_closure->do_object_b(_klasses->java_mirror()) :
   313        class_loader() == NULL || is_alive_closure->do_object_b(class_loader());
   314   assert(!alive || claimed(), "must be claimed");
   315   return alive;
   316 }
   319 ClassLoaderData::~ClassLoaderData() {
   320   // Release C heap structures for all the classes.
   321   classes_do(InstanceKlass::release_C_heap_structures);
   323   Metaspace *m = _metaspace;
   324   if (m != NULL) {
   325     _metaspace = NULL;
   326     // release the metaspace
   327     delete m;
   328     // release the handles
   329     if (_handles != NULL) {
   330       JNIHandleBlock::release_block(_handles);
   331       _handles = NULL;
   332     }
   333   }
   335   // Clear all the JNI handles for methods
   336   // These aren't deallocated and are going to look like a leak, but that's
   337   // needed because we can't really get rid of jmethodIDs because we don't
   338   // know when native code is going to stop using them.  The spec says that
   339   // they're "invalid" but existing programs likely rely on their being
   340   // NULL after class unloading.
   341   if (_jmethod_ids != NULL) {
   342     Method::clear_jmethod_ids(this);
   343   }
   344   // Delete lock
   345   delete _metaspace_lock;
   347   // Delete free list
   348   if (_deallocate_list != NULL) {
   349     delete _deallocate_list;
   350   }
   351 }
   353 /**
   354  * Returns true if this class loader data is for the extension class loader.
   355  */
   356 bool ClassLoaderData::is_ext_class_loader_data() const {
   357   return SystemDictionary::is_ext_class_loader(class_loader());
   358 }
   360 Metaspace* ClassLoaderData::metaspace_non_null() {
   361   assert(!DumpSharedSpaces, "wrong metaspace!");
   362   // If the metaspace has not been allocated, create a new one.  Might want
   363   // to create smaller arena for Reflection class loaders also.
   364   // The reason for the delayed allocation is because some class loaders are
   365   // simply for delegating with no metadata of their own.
   366   if (_metaspace == NULL) {
   367     MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   368     // Check again if metaspace has been allocated while we were getting this lock.
   369     if (_metaspace != NULL) {
   370       return _metaspace;
   371     }
   372     if (this == the_null_class_loader_data()) {
   373       assert (class_loader() == NULL, "Must be");
   374       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::BootMetaspaceType));
   375     } else if (is_anonymous()) {
   376       if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
   377         tty->print_cr("is_anonymous: %s", class_loader()->klass()->internal_name());
   378       }
   379       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType));
   380     } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
   381       if (TraceClassLoaderData && Verbose && class_loader() != NULL) {
   382         tty->print_cr("is_reflection: %s", class_loader()->klass()->internal_name());
   383       }
   384       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType));
   385     } else {
   386       set_metaspace(new Metaspace(_metaspace_lock, Metaspace::StandardMetaspaceType));
   387     }
   388   }
   389   return _metaspace;
   390 }
   392 JNIHandleBlock* ClassLoaderData::handles() const           { return _handles; }
   393 void ClassLoaderData::set_handles(JNIHandleBlock* handles) { _handles = handles; }
   395 jobject ClassLoaderData::add_handle(Handle h) {
   396   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   397   if (handles() == NULL) {
   398     set_handles(JNIHandleBlock::allocate_block());
   399   }
   400   return handles()->allocate_handle(h());
   401 }
   403 // Add this metadata pointer to be freed when it's safe.  This is only during
   404 // class unloading because Handles might point to this metadata field.
   405 void ClassLoaderData::add_to_deallocate_list(Metadata* m) {
   406   // Metadata in shared region isn't deleted.
   407   if (!m->is_shared()) {
   408     MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   409     if (_deallocate_list == NULL) {
   410       _deallocate_list = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(100, true);
   411     }
   412     _deallocate_list->append_if_missing(m);
   413   }
   414 }
   416 // Deallocate free metadata on the free list.  How useful the PermGen was!
   417 void ClassLoaderData::free_deallocate_list() {
   418   // Don't need lock, at safepoint
   419   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
   420   if (_deallocate_list == NULL) {
   421     return;
   422   }
   423   // Go backwards because this removes entries that are freed.
   424   for (int i = _deallocate_list->length() - 1; i >= 0; i--) {
   425     Metadata* m = _deallocate_list->at(i);
   426     if (!m->on_stack()) {
   427       _deallocate_list->remove_at(i);
   428       // There are only three types of metadata that we deallocate directly.
   429       // Cast them so they can be used by the template function.
   430       if (m->is_method()) {
   431         MetadataFactory::free_metadata(this, (Method*)m);
   432       } else if (m->is_constantPool()) {
   433         MetadataFactory::free_metadata(this, (ConstantPool*)m);
   434       } else if (m->is_klass()) {
   435         MetadataFactory::free_metadata(this, (InstanceKlass*)m);
   436       } else {
   437         ShouldNotReachHere();
   438       }
   439     }
   440   }
   441 }
   443 // These anonymous class loaders are to contain classes used for JSR292
   444 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(oop loader, TRAPS) {
   445   // Add a new class loader data to the graph.
   446   return ClassLoaderDataGraph::add(loader, true, CHECK_NULL);
   447 }
   449 const char* ClassLoaderData::loader_name() {
   450   // Handles null class loader
   451   return SystemDictionary::loader_name(class_loader());
   452 }
   454 #ifndef PRODUCT
   455 // Define to dump klasses
   456 #undef CLD_DUMP_KLASSES
   458 void ClassLoaderData::dump(outputStream * const out) {
   459   ResourceMark rm;
   460   out->print("ClassLoaderData CLD: "PTR_FORMAT", loader: "PTR_FORMAT", loader_klass: "PTR_FORMAT" %s {",
   461       this, (void *)class_loader(),
   462       class_loader() != NULL ? class_loader()->klass() : NULL, loader_name());
   463   if (claimed()) out->print(" claimed ");
   464   if (is_unloading()) out->print(" unloading ");
   465   out->print(" handles " INTPTR_FORMAT, handles());
   466   out->cr();
   467   if (metaspace_or_null() != NULL) {
   468     out->print_cr("metaspace: " PTR_FORMAT, metaspace_or_null());
   469     metaspace_or_null()->dump(out);
   470   } else {
   471     out->print_cr("metaspace: NULL");
   472   }
   474 #ifdef CLD_DUMP_KLASSES
   475   if (Verbose) {
   476     ResourceMark rm;
   477     Klass* k = _klasses;
   478     while (k != NULL) {
   479       out->print_cr("klass "PTR_FORMAT", %s, CT: %d, MUT: %d", k, k->name()->as_C_string(),
   480           k->has_modified_oops(), k->has_accumulated_modified_oops());
   481       assert(k != k->next_link(), "no loops!");
   482       k = k->next_link();
   483     }
   484   }
   485 #endif  // CLD_DUMP_KLASSES
   486 #undef CLD_DUMP_KLASSES
   487   if (_jmethod_ids != NULL) {
   488     Method::print_jmethod_ids(this, out);
   489   }
   490   out->print_cr("}");
   491 }
   492 #endif // PRODUCT
   494 void ClassLoaderData::verify() {
   495   oop cl = class_loader();
   497   guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same");
   498   guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be");
   500   // Verify the integrity of the allocated space.
   501   if (metaspace_or_null() != NULL) {
   502     metaspace_or_null()->verify();
   503   }
   505   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
   506     guarantee(k->class_loader_data() == this, "Must be the same");
   507     k->verify();
   508     assert(k != k->next_link(), "no loops!");
   509   }
   510 }
   513 // GC root of class loader data created.
   514 ClassLoaderData* ClassLoaderDataGraph::_head = NULL;
   515 ClassLoaderData* ClassLoaderDataGraph::_unloading = NULL;
   516 ClassLoaderData* ClassLoaderDataGraph::_saved_head = NULL;
   518 // Add a new class loader data node to the list.  Assign the newly created
   519 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
   520 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous, TRAPS) {
   521   // We need to allocate all the oops for the ClassLoaderData before allocating the
   522   // actual ClassLoaderData object.
   523   ClassLoaderData::Dependencies dependencies(CHECK_NULL);
   525   No_Safepoint_Verifier no_safepoints; // we mustn't GC until we've installed the
   526                                        // ClassLoaderData in the graph since the CLD
   527                                        // contains unhandled oops
   529   ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous, dependencies);
   532   if (!is_anonymous) {
   533     ClassLoaderData** cld_addr = java_lang_ClassLoader::loader_data_addr(loader());
   534     // First, Atomically set it
   535     ClassLoaderData* old = (ClassLoaderData*) Atomic::cmpxchg_ptr(cld, cld_addr, NULL);
   536     if (old != NULL) {
   537       delete cld;
   538       // Returns the data.
   539       return old;
   540     }
   541   }
   543   // We won the race, and therefore the task of adding the data to the list of
   544   // class loader data
   545   ClassLoaderData** list_head = &_head;
   546   ClassLoaderData* next = _head;
   548   do {
   549     cld->set_next(next);
   550     ClassLoaderData* exchanged = (ClassLoaderData*)Atomic::cmpxchg_ptr(cld, list_head, next);
   551     if (exchanged == next) {
   552       if (TraceClassLoaderData) {
   553         ResourceMark rm;
   554         tty->print("[ClassLoaderData: ");
   555         tty->print("create class loader data "PTR_FORMAT, cld);
   556         tty->print(" for instance "PTR_FORMAT" of %s", (void *)cld->class_loader(),
   557                    cld->loader_name());
   558         tty->print_cr("]");
   559       }
   560       return cld;
   561     }
   562     next = exchanged;
   563   } while (true);
   565 }
   567 void ClassLoaderDataGraph::oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
   568   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   569     cld->oops_do(f, klass_closure, must_claim);
   570   }
   571 }
   573 void ClassLoaderDataGraph::keep_alive_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
   574   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   575     if (cld->keep_alive()) {
   576       cld->oops_do(f, klass_closure, must_claim);
   577     }
   578   }
   579 }
   581 void ClassLoaderDataGraph::always_strong_oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim) {
   582   if (ClassUnloading) {
   583     ClassLoaderData::the_null_class_loader_data()->oops_do(f, klass_closure, must_claim);
   584     // keep any special CLDs alive.
   585     ClassLoaderDataGraph::keep_alive_oops_do(f, klass_closure, must_claim);
   586   } else {
   587     ClassLoaderDataGraph::oops_do(f, klass_closure, must_claim);
   588   }
   589 }
   591 void ClassLoaderDataGraph::classes_do(KlassClosure* klass_closure) {
   592   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   593     cld->classes_do(klass_closure);
   594   }
   595 }
   597 void ClassLoaderDataGraph::classes_do(void f(Klass* const)) {
   598   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   599     cld->classes_do(f);
   600   }
   601 }
   603 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
   604   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
   605   for (ClassLoaderData* cld = _unloading; cld != NULL; cld = cld->next()) {
   606     cld->classes_do(f);
   607   }
   608 }
   610 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
   611   assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
   613   GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
   615   // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
   616   ClassLoaderData* curr = _head;
   617   while (curr != _saved_head) {
   618     if (!curr->claimed()) {
   619       array->push(curr);
   621       if (TraceClassLoaderData) {
   622         tty->print("[ClassLoaderData] found new CLD: ");
   623         curr->print_value_on(tty);
   624         tty->cr();
   625       }
   626     }
   628     curr = curr->_next;
   629   }
   631   return array;
   632 }
   634 #ifndef PRODUCT
   635 // for debugging and hsfind(x)
   636 bool ClassLoaderDataGraph::contains(address x) {
   637   // I think we need the _metaspace_lock taken here because the class loader
   638   // data graph could be changing while we are walking it (new entries added,
   639   // new entries being unloaded, etc).
   640   if (DumpSharedSpaces) {
   641     // There are only two metaspaces to worry about.
   642     ClassLoaderData* ncld = ClassLoaderData::the_null_class_loader_data();
   643     return (ncld->ro_metaspace()->contains(x) || ncld->rw_metaspace()->contains(x));
   644   }
   646   if (UseSharedSpaces && MetaspaceShared::is_in_shared_space(x)) {
   647     return true;
   648   }
   650   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
   651     if (cld->metaspace_or_null() != NULL && cld->metaspace_or_null()->contains(x)) {
   652       return true;
   653     }
   654   }
   656   // Could also be on an unloading list which is okay, ie. still allocated
   657   // for a little while.
   658   for (ClassLoaderData* ucld = _unloading; ucld != NULL; ucld = ucld->next()) {
   659     if (ucld->metaspace_or_null() != NULL && ucld->metaspace_or_null()->contains(x)) {
   660       return true;
   661     }
   662   }
   663   return false;
   664 }
   666 bool ClassLoaderDataGraph::contains_loader_data(ClassLoaderData* loader_data) {
   667   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
   668     if (loader_data == data) {
   669       return true;
   670     }
   671   }
   673   return false;
   674 }
   675 #endif // PRODUCT
   678 // Move class loader data from main list to the unloaded list for unloading
   679 // and deallocation later.
   680 bool ClassLoaderDataGraph::do_unloading(BoolObjectClosure* is_alive_closure) {
   681   ClassLoaderData* data = _head;
   682   ClassLoaderData* prev = NULL;
   683   bool seen_dead_loader = false;
   684   // mark metadata seen on the stack and code cache so we can delete
   685   // unneeded entries.
   686   bool has_redefined_a_class = JvmtiExport::has_redefined_a_class();
   687   MetadataOnStackMark md_on_stack;
   688   while (data != NULL) {
   689     if (data->keep_alive() || data->is_alive(is_alive_closure)) {
   690       if (has_redefined_a_class) {
   691         data->classes_do(InstanceKlass::purge_previous_versions);
   692       }
   693       data->free_deallocate_list();
   694       prev = data;
   695       data = data->next();
   696       continue;
   697     }
   698     seen_dead_loader = true;
   699     ClassLoaderData* dead = data;
   700     dead->unload();
   701     data = data->next();
   702     // Remove from loader list.
   703     // This class loader data will no longer be found
   704     // in the ClassLoaderDataGraph.
   705     if (prev != NULL) {
   706       prev->set_next(data);
   707     } else {
   708       assert(dead == _head, "sanity check");
   709       _head = data;
   710     }
   711     dead->set_next(_unloading);
   712     _unloading = dead;
   713   }
   715   if (seen_dead_loader) {
   716     post_class_unload_events();
   717   }
   719   return seen_dead_loader;
   720 }
   722 void ClassLoaderDataGraph::purge() {
   723   ClassLoaderData* list = _unloading;
   724   _unloading = NULL;
   725   ClassLoaderData* next = list;
   726   while (next != NULL) {
   727     ClassLoaderData* purge_me = next;
   728     next = purge_me->next();
   729     delete purge_me;
   730   }
   731   Metaspace::purge();
   732 }
   734 void ClassLoaderDataGraph::post_class_unload_events(void) {
   735 #if INCLUDE_TRACE
   736   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
   737   if (Tracing::enabled()) {
   738     if (Tracing::is_event_enabled(TraceClassUnloadEvent)) {
   739       assert(_unloading != NULL, "need class loader data unload list!");
   740       _class_unload_time = Tracing::time();
   741       classes_unloading_do(&class_unload_event);
   742     }
   743     Tracing::on_unloading_classes();
   744   }
   745 #endif
   746 }
   748 // CDS support
   750 // Global metaspaces for writing information to the shared archive.  When
   751 // application CDS is supported, we may need one per metaspace, so this
   752 // sort of looks like it.
   753 Metaspace* ClassLoaderData::_ro_metaspace = NULL;
   754 Metaspace* ClassLoaderData::_rw_metaspace = NULL;
   755 static bool _shared_metaspaces_initialized = false;
   757 // Initialize shared metaspaces (change to call from somewhere not lazily)
   758 void ClassLoaderData::initialize_shared_metaspaces() {
   759   assert(DumpSharedSpaces, "only use this for dumping shared spaces");
   760   assert(this == ClassLoaderData::the_null_class_loader_data(),
   761          "only supported for null loader data for now");
   762   assert (!_shared_metaspaces_initialized, "only initialize once");
   763   MutexLockerEx ml(metaspace_lock(),  Mutex::_no_safepoint_check_flag);
   764   _ro_metaspace = new Metaspace(_metaspace_lock, Metaspace::ROMetaspaceType);
   765   _rw_metaspace = new Metaspace(_metaspace_lock, Metaspace::ReadWriteMetaspaceType);
   766   _shared_metaspaces_initialized = true;
   767 }
   769 Metaspace* ClassLoaderData::ro_metaspace() {
   770   assert(_ro_metaspace != NULL, "should already be initialized");
   771   return _ro_metaspace;
   772 }
   774 Metaspace* ClassLoaderData::rw_metaspace() {
   775   assert(_rw_metaspace != NULL, "should already be initialized");
   776   return _rw_metaspace;
   777 }
   780 ClassLoaderDataGraphMetaspaceIterator::ClassLoaderDataGraphMetaspaceIterator() {
   781   _data = ClassLoaderDataGraph::_head;
   782 }
   784 ClassLoaderDataGraphMetaspaceIterator::~ClassLoaderDataGraphMetaspaceIterator() {}
   786 #ifndef PRODUCT
   787 // callable from debugger
   788 extern "C" int print_loader_data_graph() {
   789   ClassLoaderDataGraph::dump_on(tty);
   790   return 0;
   791 }
   793 void ClassLoaderDataGraph::verify() {
   794   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
   795     data->verify();
   796   }
   797 }
   799 void ClassLoaderDataGraph::dump_on(outputStream * const out) {
   800   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
   801     data->dump(out);
   802   }
   803   MetaspaceAux::dump(out);
   804 }
   805 #endif // PRODUCT
   807 void ClassLoaderData::print_value_on(outputStream* out) const {
   808   if (class_loader() == NULL) {
   809     out->print("NULL class_loader");
   810   } else {
   811     out->print("class loader "PTR_FORMAT, this);
   812     class_loader()->print_value_on(out);
   813   }
   814 }
   816 #if INCLUDE_TRACE
   818 TracingTime ClassLoaderDataGraph::_class_unload_time;
   820 void ClassLoaderDataGraph::class_unload_event(Klass* const k) {
   822   // post class unload event
   823   EventClassUnload event(UNTIMED);
   824   event.set_endtime(_class_unload_time);
   825   event.set_unloadedClass(k);
   826   oop defining_class_loader = k->class_loader();
   827   event.set_definingClassLoader(defining_class_loader != NULL ?
   828                                 defining_class_loader->klass() : (Klass*)NULL);
   829   event.commit();
   830 }
   832 #endif /* INCLUDE_TRACE */

mercurial