< prev index next >

src/share/vm/classfile/classLoaderData.cpp

Print this page


   1 /*
   2  * Copyright (c) 2012, 2017, 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.


  32 // Class loaders that implement a deterministic name resolution strategy
  33 // (including with respect to their delegation behavior), such as the boot, the
  34 // platform, and the system loaders of the JDK's built-in class loader
  35 // hierarchy, always produce the same linkset for a given configuration.
  36 //
  37 // ClassLoaderData carries information related to a linkset (e.g.,
  38 // metaspace holding its klass definitions).
  39 // The System Dictionary and related data structures (e.g., placeholder table,
  40 // loader constraints table) as well as the runtime representation of classes
  41 // only reference ClassLoaderData.
  42 //
  43 // Instances of java.lang.ClassLoader holds a pointer to a ClassLoaderData that
  44 // that represent the loader's "linking domain" in the JVM.
  45 //
  46 // The bootstrap loader (represented by NULL) also has a ClassLoaderData,
  47 // the singleton class the_null_class_loader_data().
  48 
  49 #include "precompiled.hpp"
  50 #include "classfile/classLoaderData.hpp"
  51 #include "classfile/classLoaderData.inline.hpp"

  52 #include "classfile/javaClasses.hpp"
  53 #include "classfile/metadataOnStackMark.hpp"
  54 #include "classfile/moduleEntry.hpp"
  55 #include "classfile/packageEntry.hpp"
  56 #include "classfile/systemDictionary.hpp"
  57 #include "code/codeCache.hpp"
  58 #include "gc/shared/gcLocker.hpp"
  59 #include "logging/log.hpp"
  60 #include "memory/metadataFactory.hpp"
  61 #include "memory/metaspaceShared.hpp"
  62 #include "memory/oopFactory.hpp"
  63 #include "memory/resourceArea.hpp"
  64 #include "oops/objArrayOop.inline.hpp"
  65 #include "oops/oop.inline.hpp"
  66 #include "runtime/atomic.hpp"
  67 #include "runtime/javaCalls.hpp"
  68 #include "runtime/jniHandles.hpp"
  69 #include "runtime/mutex.hpp"
  70 #include "runtime/orderAccess.hpp"
  71 #include "runtime/safepoint.hpp"


  96   _claimed(0), _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),
  97   _next(NULL), _dependencies(dependencies),
  98   _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true,
  99                             Monitor::_safepoint_check_never)) {
 100 
 101   // A ClassLoaderData created solely for an anonymous class should never have a
 102   // ModuleEntryTable or PackageEntryTable created for it. The defining package
 103   // and module for an anonymous class will be found in its host class.
 104   if (!is_anonymous) {
 105     _packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size);
 106     if (h_class_loader.is_null()) {
 107       // Create unnamed module for boot loader
 108       _unnamed_module = ModuleEntry::create_boot_unnamed_module(this);
 109     } else {
 110       // Create unnamed module for all other loaders
 111       _unnamed_module = ModuleEntry::create_unnamed_module(this);
 112     }
 113   } else {
 114     _unnamed_module = NULL;
 115   }






 116   TRACE_INIT_ID(this);
 117 }
 118 
 119 void ClassLoaderData::init_dependencies(TRAPS) {
 120   assert(!Universe::is_fully_initialized(), "should only be called when initializing");
 121   assert(is_the_null_class_loader_data(), "should only call this for the null class loader");
 122   _dependencies.init(CHECK);
 123 }
 124 
 125 void ClassLoaderData::Dependencies::init(TRAPS) {
 126   // Create empty dependencies array to add to. CMS requires this to be
 127   // an oop so that it can track additions via card marks.  We think.
 128   _list_head = oopFactory::new_objectArray(2, CHECK);
 129 }
 130 
 131 ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() {
 132   Chunk* c = _head;
 133   while (c != NULL) {
 134     Chunk* next = c->_next;
 135     delete c;


 432     MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
 433     Klass* old_value = _klasses;
 434     k->set_next_link(old_value);
 435     // Link the new item into the list, making sure the linked class is stable
 436     // since the list can be walked without a lock
 437     OrderAccess::release_store_ptr(&_klasses, k);
 438   }
 439 
 440   if (publicize && k->class_loader_data() != NULL) {
 441     ResourceMark rm;
 442     log_trace(class, loader, data)("Adding k: " PTR_FORMAT " %s to CLD: "
 443                   PTR_FORMAT " loader: " PTR_FORMAT " %s",
 444                   p2i(k),
 445                   k->external_name(),
 446                   p2i(k->class_loader_data()),
 447                   p2i((void *)k->class_loader()),
 448                   loader_name());
 449   }
 450 }
 451 

































































 452 // Remove a klass from the _klasses list for scratch_class during redefinition
 453 // or parsed class in the case of an error.
 454 void ClassLoaderData::remove_class(Klass* scratch_class) {
 455   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");




 456   Klass* prev = NULL;
 457   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
 458     if (k == scratch_class) {
 459       if (prev == NULL) {
 460         _klasses = k->next_link();
 461       } else {
 462         Klass* next = k->next_link();
 463         prev->set_next_link(next);
 464       }
 465       return;
 466     }
 467     prev = k;
 468     assert(k != k->next_link(), "no loops!");
 469   }
 470   ShouldNotReachHere();   // should have found this class!!
 471 }
 472 
 473 void ClassLoaderData::unload() {
 474   _unloading = true;
 475 
 476   // Tell serviceability tools these classes are unloading
 477   classes_do(InstanceKlass::notify_unload_class);
 478 
 479   if (log_is_enabled(Debug, class, loader, data)) {
 480     ResourceMark rm;
 481     outputStream* log = Log(class, loader, data)::debug_stream();
 482     log->print(": unload loader data " INTPTR_FORMAT, p2i(this));
 483     log->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)class_loader()),
 484                loader_name());
 485     if (is_anonymous()) {
 486       log->print(" for anonymous class  " INTPTR_FORMAT " ", p2i(_klasses));
 487     }
 488     log->cr();
 489   }
 490 
 491   // In some rare cases items added to this list will not be freed elsewhere.
 492   // To keep it simple, just free everything in it here.
 493   free_deallocate_list();



 494 }
 495 
 496 ModuleEntryTable* ClassLoaderData::modules() {
 497   // Lazily create the module entry table at first request.
 498   // Lock-free access requires load_ptr_acquire.
 499   ModuleEntryTable* modules = load_ptr_acquire(&_modules);
 500   if (modules == NULL) {
 501     MutexLocker m1(Module_lock);
 502     // Check if _modules got allocated while we were waiting for this lock.
 503     if ((modules = _modules) == NULL) {
 504       modules = new ModuleEntryTable(ModuleEntryTable::_moduletable_entry_size);
 505 
 506       {
 507         MutexLockerEx m1(metaspace_lock(), Mutex::_no_safepoint_check_flag);
 508         // Ensure _modules is stable, since it is examined without a lock
 509         OrderAccess::release_store_ptr(&_modules, modules);
 510       }
 511     }
 512   }
 513   return modules;
 514 }
 515 







































 516 oop ClassLoaderData::keep_alive_object() const {
 517   assert_locked_or_safepoint(_metaspace_lock);
 518   assert(!keep_alive(), "Don't use with CLDs that are artificially kept alive");
 519   return is_anonymous() ? _klasses->java_mirror() : class_loader();
 520 }
 521 
 522 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {
 523   bool alive = keep_alive() // null class loader and incomplete anonymous klasses.
 524       || is_alive_closure->do_object_b(keep_alive_object());
 525 
 526   return alive;
 527 }
 528 
 529 ClassLoaderData::~ClassLoaderData() {
 530   // Release C heap structures for all the classes.
 531   classes_do(InstanceKlass::release_C_heap_structures);
 532 
 533   // Release C heap allocated hashtable for all the packages.
 534   if (_packages != NULL) {
 535     // Destroy the table itself
 536     delete _packages;
 537     _packages = NULL;
 538   }
 539 
 540   // Release C heap allocated hashtable for all the modules.
 541   if (_modules != NULL) {
 542     // Destroy the table itself
 543     delete _modules;
 544     _modules = NULL;
 545   }
 546 







 547   if (_unnamed_module != NULL) {
 548     _unnamed_module->delete_unnamed_module();
 549     _unnamed_module = NULL;
 550   }
 551 
 552   // release the metaspace
 553   Metaspace *m = _metaspace;
 554   if (m != NULL) {
 555     _metaspace = NULL;
 556     delete m;
 557   }
 558   // Clear all the JNI handles for methods
 559   // These aren't deallocated and are going to look like a leak, but that's
 560   // needed because we can't really get rid of jmethodIDs because we don't
 561   // know when native code is going to stop using them.  The spec says that
 562   // they're "invalid" but existing programs likely rely on their being
 563   // NULL after class unloading.
 564   if (_jmethod_ids != NULL) {
 565     Method::clear_jmethod_ids(this);
 566   }


 954     cld->packages_do(f);
 955   }
 956 }
 957 
 958 void ClassLoaderDataGraph::loaded_classes_do(KlassClosure* klass_closure) {
 959   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
 960     cld->loaded_classes_do(klass_closure);
 961   }
 962 }
 963 
 964 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
 965   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
 966   // Only walk the head until any clds not purged from prior unloading
 967   // (CMS doesn't purge right away).
 968   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
 969     assert(cld->is_unloading(), "invariant");
 970     cld->classes_do(f);
 971   }
 972 }
 973 








































 974 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
 975   assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
 976 
 977   GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
 978 
 979   // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
 980   ClassLoaderData* curr = _head;
 981   while (curr != _saved_head) {
 982     if (!curr->claimed()) {
 983       array->push(curr);
 984 
 985       if (log_is_enabled(Debug, class, loader, data)) {
 986         outputStream* log = Log(class, loader, data)::debug_stream();
 987         log->print("found new CLD: ");
 988         curr->print_value_on(log);
 989         log->cr();
 990       }
 991     }
 992 
 993     curr = curr->_next;


1054       continue;
1055     }
1056     seen_dead_loader = true;
1057     ClassLoaderData* dead = data;
1058     dead->unload();
1059     data = data->next();
1060     // Remove from loader list.
1061     // This class loader data will no longer be found
1062     // in the ClassLoaderDataGraph.
1063     if (prev != NULL) {
1064       prev->set_next(data);
1065     } else {
1066       assert(dead == _head, "sanity check");
1067       _head = data;
1068     }
1069     dead->set_next(_unloading);
1070     _unloading = dead;
1071   }
1072 
1073   if (seen_dead_loader) {
1074     // Walk a ModuleEntry's reads and a PackageEntry's exports lists
1075     // to determine if there are modules on those lists that are now







1076     // dead and should be removed.  A module's life cycle is equivalent
1077     // to its defining class loader's life cycle.  Since a module is
1078     // considered dead if its class loader is dead, these walks must
1079     // occur after each class loader's aliveness is determined.
1080     data = _head;
1081     while (data != NULL) {
1082       if (data->packages() != NULL) {
1083         data->packages()->purge_all_package_exports();
1084       }
1085       if (data->modules_defined()) {
1086         data->modules()->purge_all_module_reads();
1087       }
1088       data = data->next();
1089     }
1090 
1091     post_class_unload_events();
1092   }
1093 
1094   return seen_dead_loader;
1095 }
1096 
1097 void ClassLoaderDataGraph::purge() {
1098   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
1099   ClassLoaderData* list = _unloading;
1100   _unloading = NULL;
1101   ClassLoaderData* next = list;


1230 void ClassLoaderDataGraph::verify() {
1231   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
1232     data->verify();
1233   }
1234 }
1235 
1236 void ClassLoaderDataGraph::dump_on(outputStream * const out) {
1237   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
1238     data->dump(out);
1239   }
1240   MetaspaceAux::dump(out);
1241 }
1242 #endif // PRODUCT
1243 
1244 void ClassLoaderData::print_value_on(outputStream* out) const {
1245   if (class_loader() == NULL) {
1246     out->print("NULL class_loader");
1247   } else {
1248     out->print("class loader " INTPTR_FORMAT " ", p2i(this));
1249     class_loader()->print_value_on(out);









1250   }
1251 }
1252 
1253 #if INCLUDE_TRACE
1254 
1255 Ticks ClassLoaderDataGraph::_class_unload_time;
1256 
1257 void ClassLoaderDataGraph::class_unload_event(Klass* const k) {
1258   assert(k != NULL, "invariant");
1259 
1260   // post class unload event
1261   EventClassUnload event(UNTIMED);
1262   event.set_endtime(_class_unload_time);
1263   event.set_unloadedClass(k);
1264   event.set_definingClassLoader(k->class_loader_data());
1265   event.commit();
1266 }
1267 
1268 #endif // INCLUDE_TRACE
   1  /*
   2  * Copyright (c) 2012, 2017, 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.


  32 // Class loaders that implement a deterministic name resolution strategy
  33 // (including with respect to their delegation behavior), such as the boot, the
  34 // platform, and the system loaders of the JDK's built-in class loader
  35 // hierarchy, always produce the same linkset for a given configuration.
  36 //
  37 // ClassLoaderData carries information related to a linkset (e.g.,
  38 // metaspace holding its klass definitions).
  39 // The System Dictionary and related data structures (e.g., placeholder table,
  40 // loader constraints table) as well as the runtime representation of classes
  41 // only reference ClassLoaderData.
  42 //
  43 // Instances of java.lang.ClassLoader holds a pointer to a ClassLoaderData that
  44 // that represent the loader's "linking domain" in the JVM.
  45 //
  46 // The bootstrap loader (represented by NULL) also has a ClassLoaderData,
  47 // the singleton class the_null_class_loader_data().
  48 
  49 #include "precompiled.hpp"
  50 #include "classfile/classLoaderData.hpp"
  51 #include "classfile/classLoaderData.inline.hpp"
  52 #include "classfile/dictionary.hpp"
  53 #include "classfile/javaClasses.hpp"
  54 #include "classfile/metadataOnStackMark.hpp"
  55 #include "classfile/moduleEntry.hpp"
  56 #include "classfile/packageEntry.hpp"
  57 #include "classfile/systemDictionary.hpp"
  58 #include "code/codeCache.hpp"
  59 #include "gc/shared/gcLocker.hpp"
  60 #include "logging/log.hpp"
  61 #include "memory/metadataFactory.hpp"
  62 #include "memory/metaspaceShared.hpp"
  63 #include "memory/oopFactory.hpp"
  64 #include "memory/resourceArea.hpp"
  65 #include "oops/objArrayOop.inline.hpp"
  66 #include "oops/oop.inline.hpp"
  67 #include "runtime/atomic.hpp"
  68 #include "runtime/javaCalls.hpp"
  69 #include "runtime/jniHandles.hpp"
  70 #include "runtime/mutex.hpp"
  71 #include "runtime/orderAccess.hpp"
  72 #include "runtime/safepoint.hpp"


  97   _claimed(0), _jmethod_ids(NULL), _handles(), _deallocate_list(NULL),
  98   _next(NULL), _dependencies(dependencies),
  99   _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true,
 100                             Monitor::_safepoint_check_never)) {
 101 
 102   // A ClassLoaderData created solely for an anonymous class should never have a
 103   // ModuleEntryTable or PackageEntryTable created for it. The defining package
 104   // and module for an anonymous class will be found in its host class.
 105   if (!is_anonymous) {
 106     _packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size);
 107     if (h_class_loader.is_null()) {
 108       // Create unnamed module for boot loader
 109       _unnamed_module = ModuleEntry::create_boot_unnamed_module(this);
 110     } else {
 111       // Create unnamed module for all other loaders
 112       _unnamed_module = ModuleEntry::create_unnamed_module(this);
 113     }
 114   } else {
 115     _unnamed_module = NULL;
 116   }
 117 
 118   if (!is_anonymous) {
 119     _dictionary = create_dictionary();
 120   } else {
 121     _dictionary = NULL;
 122   }
 123   TRACE_INIT_ID(this);
 124 }
 125 
 126 void ClassLoaderData::init_dependencies(TRAPS) {
 127   assert(!Universe::is_fully_initialized(), "should only be called when initializing");
 128   assert(is_the_null_class_loader_data(), "should only call this for the null class loader");
 129   _dependencies.init(CHECK);
 130 }
 131 
 132 void ClassLoaderData::Dependencies::init(TRAPS) {
 133   // Create empty dependencies array to add to. CMS requires this to be
 134   // an oop so that it can track additions via card marks.  We think.
 135   _list_head = oopFactory::new_objectArray(2, CHECK);
 136 }
 137 
 138 ClassLoaderData::ChunkedHandleList::~ChunkedHandleList() {
 139   Chunk* c = _head;
 140   while (c != NULL) {
 141     Chunk* next = c->_next;
 142     delete c;


 439     MutexLockerEx ml(metaspace_lock(), Mutex::_no_safepoint_check_flag);
 440     Klass* old_value = _klasses;
 441     k->set_next_link(old_value);
 442     // Link the new item into the list, making sure the linked class is stable
 443     // since the list can be walked without a lock
 444     OrderAccess::release_store_ptr(&_klasses, k);
 445   }
 446 
 447   if (publicize && k->class_loader_data() != NULL) {
 448     ResourceMark rm;
 449     log_trace(class, loader, data)("Adding k: " PTR_FORMAT " %s to CLD: "
 450                   PTR_FORMAT " loader: " PTR_FORMAT " %s",
 451                   p2i(k),
 452                   k->external_name(),
 453                   p2i(k->class_loader_data()),
 454                   p2i((void *)k->class_loader()),
 455                   loader_name());
 456   }
 457 }
 458 
 459 // Class iterator used by the compiler.  It gets some number of classes at
 460 // a safepoint to decay invocation counters on the methods.
 461 class ClassLoaderDataGraphKlassIteratorStatic {
 462   ClassLoaderData* _current_loader_data;
 463   Klass*           _current_class_entry;
 464  public:
 465 
 466   ClassLoaderDataGraphKlassIteratorStatic() : _current_loader_data(NULL), _current_class_entry(NULL) {}
 467 
 468   InstanceKlass* try_get_next_class() {
 469     assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
 470     while (true) {
 471   
 472       if (_current_class_entry != NULL) {
 473         Klass* k = _current_class_entry;
 474         _current_class_entry = _current_class_entry->next_link();
 475   
 476         if (k->is_instance_klass()) {
 477           InstanceKlass* ik = InstanceKlass::cast(k);
 478           // Only return loaded classes
 479           if (ik->is_loaded()) {
 480             return ik;
 481           }
 482         }
 483       } else {
 484         // Go to next CLD
 485         if (_current_loader_data != NULL) {
 486           _current_loader_data = _current_loader_data->next();
 487         }
 488         // Start at the beginning
 489         if (_current_loader_data == NULL) {
 490           _current_loader_data = ClassLoaderDataGraph::_head;
 491         }
 492   
 493         _current_class_entry = _current_loader_data->klasses();
 494       }
 495     }
 496     // never reached: an InstanceKlass should be returned above
 497   }
 498 
 499   // If the current class for the static iterator is a class being unloaded or
 500   // deallocated, adjust the current class.
 501   void adjust_saved_class(ClassLoaderData* cld) {
 502     if (_current_loader_data == cld) {
 503       _current_loader_data = cld->next();
 504       if (_current_loader_data != NULL) {
 505         _current_class_entry = _current_loader_data->klasses();
 506       }  // else try_get_next_class will start at the head
 507     }
 508   }
 509 
 510   void adjust_saved_class(Klass* klass) {
 511     if (_current_class_entry == klass) {
 512       _current_class_entry = klass->next_link();
 513     }
 514   }
 515 };
 516 
 517 static ClassLoaderDataGraphKlassIteratorStatic static_klass_iterator;
 518 
 519 InstanceKlass* ClassLoaderDataGraph::try_get_next_class() {
 520   return static_klass_iterator.try_get_next_class();
 521 }
 522 
 523 
 524 // Remove a klass from the _klasses list for scratch_class during redefinition
 525 // or parsed class in the case of an error.
 526 void ClassLoaderData::remove_class(Klass* scratch_class) {
 527   assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
 528 
 529   // Adjust global class iterator.
 530   static_klass_iterator.adjust_saved_class(scratch_class);
 531 
 532   Klass* prev = NULL;
 533   for (Klass* k = _klasses; k != NULL; k = k->next_link()) {
 534     if (k == scratch_class) {
 535       if (prev == NULL) {
 536         _klasses = k->next_link();
 537       } else {
 538         Klass* next = k->next_link();
 539         prev->set_next_link(next);
 540       }
 541       return;
 542     }
 543     prev = k;
 544     assert(k != k->next_link(), "no loops!");
 545   }
 546   ShouldNotReachHere();   // should have found this class!!
 547 }
 548 
 549 void ClassLoaderData::unload() {
 550   _unloading = true;
 551 
 552   // Tell serviceability tools these classes are unloading
 553   classes_do(InstanceKlass::notify_unload_class);
 554 
 555   if (log_is_enabled(Debug, class, loader, data)) {
 556     ResourceMark rm;
 557     outputStream* log = Log(class, loader, data)::debug_stream();
 558     log->print(": unload loader data " INTPTR_FORMAT, p2i(this));
 559     log->print(" for instance " INTPTR_FORMAT " of %s", p2i((void *)class_loader()),
 560                loader_name());
 561     if (is_anonymous()) {
 562       log->print(" for anonymous class  " INTPTR_FORMAT " ", p2i(_klasses));
 563     }
 564     log->cr();
 565   }
 566 
 567   // In some rare cases items added to this list will not be freed elsewhere.
 568   // To keep it simple, just free everything in it here.
 569   free_deallocate_list();
 570 
 571   // Clean up global class iterator for compiler
 572   static_klass_iterator.adjust_saved_class(this);
 573 }
 574 
 575 ModuleEntryTable* ClassLoaderData::modules() {
 576   // Lazily create the module entry table at first request.
 577   // Lock-free access requires load_ptr_acquire.
 578   ModuleEntryTable* modules = load_ptr_acquire(&_modules);
 579   if (modules == NULL) {
 580     MutexLocker m1(Module_lock);
 581     // Check if _modules got allocated while we were waiting for this lock.
 582     if ((modules = _modules) == NULL) {
 583       modules = new ModuleEntryTable(ModuleEntryTable::_moduletable_entry_size);
 584 
 585       {
 586         MutexLockerEx m1(metaspace_lock(), Mutex::_no_safepoint_check_flag);
 587         // Ensure _modules is stable, since it is examined without a lock
 588         OrderAccess::release_store_ptr(&_modules, modules);
 589       }
 590     }
 591   }
 592   return modules;
 593 }
 594 
 595 const int _boot_loader_dictionary_size    = 1009;
 596 const int _default_loader_dictionary_size = 107;
 597 const int _prime_array_size         = 8;                       // array of primes for system dictionary size
 598 const int _average_depth_goal       = 3;                       // goal for lookup length
 599 const int _primelist[_prime_array_size] = {107, 1009, 2017, 4049, 5051, 10103, 20201, 40423};
 600 
 601 // Calculate a "good" dictionary size based
 602 // on predicted or current loaded classes count.
 603 static int calculate_dictionary_size(int classcount) {
 604   int newsize = _primelist[0];
 605   if (classcount > 0 && !DumpSharedSpaces) {
 606     int index = 0;
 607     int desiredsize = classcount/_average_depth_goal;
 608     for (newsize = _primelist[index]; index < _prime_array_size -1;
 609          newsize = _primelist[++index]) {
 610       if (desiredsize <=  newsize) {
 611         break;
 612       }
 613     }
 614   }
 615   return newsize;
 616 }
 617 
 618 Dictionary* ClassLoaderData::create_dictionary() {
 619   assert(!is_anonymous(), "anonymous class loader data do not have a dictionary");
 620   int size;
 621   if (_the_null_class_loader_data == NULL) {
 622     size = _boot_loader_dictionary_size;
 623   } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
 624     size = 1;  // there's only one class in relection class loader and no initiated classes
 625   } else if (is_system_class_loader_data()) {
 626     size = calculate_dictionary_size(PredictedLoadedClassCount);
 627   } else {
 628     size = _default_loader_dictionary_size;
 629   }
 630   return new Dictionary(this, size);
 631 }
 632 
 633 // Unloading support
 634 oop ClassLoaderData::keep_alive_object() const {
 635   assert_locked_or_safepoint(_metaspace_lock);
 636   assert(!keep_alive(), "Don't use with CLDs that are artificially kept alive");
 637   return is_anonymous() ? _klasses->java_mirror() : class_loader();
 638 }
 639 
 640 bool ClassLoaderData::is_alive(BoolObjectClosure* is_alive_closure) const {
 641   bool alive = keep_alive() // null class loader and incomplete anonymous klasses.
 642       || is_alive_closure->do_object_b(keep_alive_object());
 643 
 644   return alive;
 645 }
 646 
 647 ClassLoaderData::~ClassLoaderData() {
 648   // Release C heap structures for all the classes.
 649   classes_do(InstanceKlass::release_C_heap_structures);
 650 
 651   // Release C heap allocated hashtable for all the packages.
 652   if (_packages != NULL) {
 653     // Destroy the table itself
 654     delete _packages;
 655     _packages = NULL;
 656   }
 657 
 658   // Release C heap allocated hashtable for all the modules.
 659   if (_modules != NULL) {
 660     // Destroy the table itself
 661     delete _modules;
 662     _modules = NULL;
 663   }
 664 
 665   // Release C heap allocated hashtable for the dictionary
 666   if (_dictionary != NULL) {
 667     // Destroy the table itself
 668     delete _dictionary;
 669     _dictionary = NULL;
 670   }
 671 
 672   if (_unnamed_module != NULL) {
 673     _unnamed_module->delete_unnamed_module();
 674     _unnamed_module = NULL;
 675   }
 676 
 677   // release the metaspace
 678   Metaspace *m = _metaspace;
 679   if (m != NULL) {
 680     _metaspace = NULL;
 681     delete m;
 682   }
 683   // Clear all the JNI handles for methods
 684   // These aren't deallocated and are going to look like a leak, but that's
 685   // needed because we can't really get rid of jmethodIDs because we don't
 686   // know when native code is going to stop using them.  The spec says that
 687   // they're "invalid" but existing programs likely rely on their being
 688   // NULL after class unloading.
 689   if (_jmethod_ids != NULL) {
 690     Method::clear_jmethod_ids(this);
 691   }


1079     cld->packages_do(f);
1080   }
1081 }
1082 
1083 void ClassLoaderDataGraph::loaded_classes_do(KlassClosure* klass_closure) {
1084   for (ClassLoaderData* cld = _head; cld != NULL; cld = cld->next()) {
1085     cld->loaded_classes_do(klass_closure);
1086   }
1087 }
1088 
1089 void ClassLoaderDataGraph::classes_unloading_do(void f(Klass* const)) {
1090   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
1091   // Only walk the head until any clds not purged from prior unloading
1092   // (CMS doesn't purge right away).
1093   for (ClassLoaderData* cld = _unloading; cld != _saved_unloading; cld = cld->next()) {
1094     assert(cld->is_unloading(), "invariant");
1095     cld->classes_do(f);
1096   }
1097 }
1098 
1099 #define FOR_ALL_DICTIONARY(X) for (ClassLoaderData* X = _head; X != NULL; X = X->next()) \
1100                                 if (X->dictionary() != NULL)
1101 
1102 // Walk classes in the loaded class dictionaries in various forms.
1103 // Only walks the classes defined in this class loader.
1104 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*)) {
1105   FOR_ALL_DICTIONARY(cld) {
1106     cld->dictionary()->classes_do(f);
1107   }
1108 }
1109 
1110 // Only walks the classes defined in this class loader.
1111 void ClassLoaderDataGraph::dictionary_classes_do(void f(InstanceKlass*, TRAPS), TRAPS) {
1112   FOR_ALL_DICTIONARY(cld) {
1113     cld->dictionary()->classes_do(f, CHECK);
1114   }
1115 }
1116 
1117 // Walks all entries in the dictionary including entries initiated by this class loader.
1118 void ClassLoaderDataGraph::dictionary_all_entries_do(void f(InstanceKlass*, ClassLoaderData*)) {
1119   FOR_ALL_DICTIONARY(cld) {
1120     cld->dictionary()->all_entries_do(f);
1121   }
1122 }
1123 
1124 void ClassLoaderDataGraph::verify_dictionary() {
1125   FOR_ALL_DICTIONARY(cld) {
1126     cld->dictionary()->verify();
1127   }
1128 }
1129 
1130 void ClassLoaderDataGraph::print_dictionary(bool details) {
1131   FOR_ALL_DICTIONARY(cld) {
1132     tty->print("Dictionary for class loader ");
1133     cld->print_value();
1134     tty->cr();
1135     cld->dictionary()->print(details);
1136   }
1137 }
1138 
1139 GrowableArray<ClassLoaderData*>* ClassLoaderDataGraph::new_clds() {
1140   assert(_head == NULL || _saved_head != NULL, "remember_new_clds(true) not called?");
1141 
1142   GrowableArray<ClassLoaderData*>* array = new GrowableArray<ClassLoaderData*>();
1143 
1144   // The CLDs in [_head, _saved_head] were all added during last call to remember_new_clds(true);
1145   ClassLoaderData* curr = _head;
1146   while (curr != _saved_head) {
1147     if (!curr->claimed()) {
1148       array->push(curr);
1149 
1150       if (log_is_enabled(Debug, class, loader, data)) {
1151         outputStream* log = Log(class, loader, data)::debug_stream();
1152         log->print("found new CLD: ");
1153         curr->print_value_on(log);
1154         log->cr();
1155       }
1156     }
1157 
1158     curr = curr->_next;


1219       continue;
1220     }
1221     seen_dead_loader = true;
1222     ClassLoaderData* dead = data;
1223     dead->unload();
1224     data = data->next();
1225     // Remove from loader list.
1226     // This class loader data will no longer be found
1227     // in the ClassLoaderDataGraph.
1228     if (prev != NULL) {
1229       prev->set_next(data);
1230     } else {
1231       assert(dead == _head, "sanity check");
1232       _head = data;
1233     }
1234     dead->set_next(_unloading);
1235     _unloading = dead;
1236   }
1237 
1238   if (seen_dead_loader) {
1239     data = _head;
1240     while (data != NULL) {
1241       // Remove entries in the dictionary of live class loader that have
1242       // initiated loading classes in a dead class loader.
1243       if (data->dictionary() != NULL) {
1244         data->dictionary()->do_unloading();
1245       }
1246       // Walk a ModuleEntry's reads, and a PackageEntry's exports
1247       // lists to determine if there are modules on those lists that are now
1248       // dead and should be removed.  A module's life cycle is equivalent
1249       // to its defining class loader's life cycle.  Since a module is
1250       // considered dead if its class loader is dead, these walks must
1251       // occur after each class loader's aliveness is determined.


1252       if (data->packages() != NULL) {
1253         data->packages()->purge_all_package_exports();
1254       }
1255       if (data->modules_defined()) {
1256         data->modules()->purge_all_module_reads();
1257       }
1258       data = data->next();
1259     }
1260 
1261     post_class_unload_events();
1262   }
1263 
1264   return seen_dead_loader;
1265 }
1266 
1267 void ClassLoaderDataGraph::purge() {
1268   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
1269   ClassLoaderData* list = _unloading;
1270   _unloading = NULL;
1271   ClassLoaderData* next = list;


1400 void ClassLoaderDataGraph::verify() {
1401   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
1402     data->verify();
1403   }
1404 }
1405 
1406 void ClassLoaderDataGraph::dump_on(outputStream * const out) {
1407   for (ClassLoaderData* data = _head; data != NULL; data = data->next()) {
1408     data->dump(out);
1409   }
1410   MetaspaceAux::dump(out);
1411 }
1412 #endif // PRODUCT
1413 
1414 void ClassLoaderData::print_value_on(outputStream* out) const {
1415   if (class_loader() == NULL) {
1416     out->print("NULL class_loader");
1417   } else {
1418     out->print("class loader " INTPTR_FORMAT " ", p2i(this));
1419     class_loader()->print_value_on(out);
1420   }
1421 }
1422 
1423 void ClassLoaderData::print_on(outputStream* out) const {
1424   if (class_loader() == NULL) {
1425     out->print("NULL class_loader");
1426   } else {
1427     out->print("class loader " INTPTR_FORMAT " ", p2i(this));
1428     class_loader()->print_on(out);
1429   }
1430 }
1431 
1432 #if INCLUDE_TRACE
1433 
1434 Ticks ClassLoaderDataGraph::_class_unload_time;
1435 
1436 void ClassLoaderDataGraph::class_unload_event(Klass* const k) {
1437   assert(k != NULL, "invariant");
1438 
1439   // post class unload event
1440   EventClassUnload event(UNTIMED);
1441   event.set_endtime(_class_unload_time);
1442   event.set_unloadedClass(k);
1443   event.set_definingClassLoader(k->class_loader_data());
1444   event.commit();
1445 }
1446 
1447 #endif // INCLUDE_TRACE
< prev index next >