< prev index next >

src/share/vm/classfile/classLoaderData.cpp

Print this page




  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"


  75 #include "utilities/ostream.hpp"
  76 #if INCLUDE_TRACE
  77 #include "trace/tracing.hpp"
  78 #endif
  79 
  80 // helper function to avoid in-line casts
  81 template <typename T> static T* load_ptr_acquire(T* volatile *p) {
  82   return static_cast<T*>(OrderAccess::load_ptr_acquire(p));
  83 }
  84 
  85 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
  86 
  87 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) :
  88   _class_loader(h_class_loader()),
  89   _is_anonymous(is_anonymous),
  90   // An anonymous class loader data doesn't have anything to keep
  91   // it from being unloaded during parsing of the anonymous class.
  92   // The null-class-loader should always be kept alive.
  93   _keep_alive((is_anonymous || h_class_loader.is_null()) ? 1 : 0),
  94   _metaspace(NULL), _unloading(false), _klasses(NULL),
  95   _modules(NULL), _packages(NULL),
  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   }


 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


  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"


  76 #include "utilities/ostream.hpp"
  77 #if INCLUDE_TRACE
  78 #include "trace/tracing.hpp"
  79 #endif
  80 
  81 // helper function to avoid in-line casts
  82 template <typename T> static T* load_ptr_acquire(T* volatile *p) {
  83   return static_cast<T*>(OrderAccess::load_ptr_acquire(p));
  84 }
  85 
  86 ClassLoaderData * ClassLoaderData::_the_null_class_loader_data = NULL;
  87 
  88 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies) :
  89   _class_loader(h_class_loader()),
  90   _is_anonymous(is_anonymous),
  91   // An anonymous class loader data doesn't have anything to keep
  92   // it from being unloaded during parsing of the anonymous class.
  93   // The null-class-loader should always be kept alive.
  94   _keep_alive((is_anonymous || h_class_loader.is_null()) ? 1 : 0),
  95   _metaspace(NULL), _unloading(false), _klasses(NULL),
  96   _modules(NULL), _packages(NULL), _dictionary(NULL),
  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   }


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


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


1228       continue;
1229     }
1230     seen_dead_loader = true;
1231     ClassLoaderData* dead = data;
1232     dead->unload();
1233     data = data->next();
1234     // Remove from loader list.
1235     // This class loader data will no longer be found
1236     // in the ClassLoaderDataGraph.
1237     if (prev != NULL) {
1238       prev->set_next(data);
1239     } else {
1240       assert(dead == _head, "sanity check");
1241       _head = data;
1242     }
1243     dead->set_next(_unloading);
1244     _unloading = dead;
1245   }
1246 
1247   if (seen_dead_loader) {
1248     // Walk a Dictionary, ModuleEntry's reads, and a PackageEntry's exports
1249     // lists to determine if there are modules on those lists that are now
1250     // dead and should be removed.  A module's life cycle is equivalent
1251     // to its defining class loader's life cycle.  Since a module is
1252     // considered dead if its class loader is dead, these walks must
1253     // occur after each class loader's aliveness is determined.
1254     data = _head;
1255     while (data != NULL) {
1256       if (data->dictionary_or_null() != NULL) {
1257         data->dictionary()->do_unloading();
1258       }
1259       if (data->packages() != NULL) {
1260         data->packages()->purge_all_package_exports();
1261       }
1262       if (data->modules_defined()) {
1263         data->modules()->purge_all_module_reads();
1264       }
1265       data = data->next();
1266     }
1267 
1268     post_class_unload_events();
1269   }
1270 
1271   return seen_dead_loader;
1272 }
1273 
1274 void ClassLoaderDataGraph::purge() {
1275   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
1276   ClassLoaderData* list = _unloading;
1277   _unloading = NULL;
1278   ClassLoaderData* next = list;


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