src/hotspot/share/classfile/classLoaderData.cpp
changeset 51444 3e5d28e6de32
parent 51415 0aa5378d4f09
child 51470 84d3126858d5
equal deleted inserted replaced
51443:cdffba164671 51444:3e5d28e6de32
   139   assert(cl_instance_name_and_id != NULL && cl_instance_name_and_id[0] != '\0', "class loader has no name and id");
   139   assert(cl_instance_name_and_id != NULL && cl_instance_name_and_id[0] != '\0', "class loader has no name and id");
   140   // Can't throw InternalError and SymbolTable doesn't throw OOM anymore.
   140   // Can't throw InternalError and SymbolTable doesn't throw OOM anymore.
   141   _name_and_id = SymbolTable::new_symbol(cl_instance_name_and_id, CATCH);
   141   _name_and_id = SymbolTable::new_symbol(cl_instance_name_and_id, CATCH);
   142 }
   142 }
   143 
   143 
   144 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_anonymous) :
   144 ClassLoaderData::ClassLoaderData(Handle h_class_loader, bool is_unsafe_anonymous) :
   145   _metaspace(NULL),
   145   _metaspace(NULL),
   146   _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true,
   146   _metaspace_lock(new Mutex(Monitor::leaf+1, "Metaspace allocation lock", true,
   147                             Monitor::_safepoint_check_never)),
   147                             Monitor::_safepoint_check_never)),
   148   _unloading(false), _is_anonymous(is_anonymous),
   148   _unloading(false), _is_unsafe_anonymous(is_unsafe_anonymous),
   149   _modified_oops(true), _accumulated_modified_oops(false),
   149   _modified_oops(true), _accumulated_modified_oops(false),
   150   // An anonymous class loader data doesn't have anything to keep
   150   // An unsafe anonymous class loader data doesn't have anything to keep
   151   // it from being unloaded during parsing of the anonymous class.
   151   // it from being unloaded during parsing of the unsafe anonymous class.
   152   // The null-class-loader should always be kept alive.
   152   // The null-class-loader should always be kept alive.
   153   _keep_alive((is_anonymous || h_class_loader.is_null()) ? 1 : 0),
   153   _keep_alive((is_unsafe_anonymous || h_class_loader.is_null()) ? 1 : 0),
   154   _claimed(0),
   154   _claimed(0),
   155   _handles(),
   155   _handles(),
   156   _klasses(NULL), _packages(NULL), _modules(NULL), _unnamed_module(NULL), _dictionary(NULL),
   156   _klasses(NULL), _packages(NULL), _modules(NULL), _unnamed_module(NULL), _dictionary(NULL),
   157   _jmethod_ids(NULL),
   157   _jmethod_ids(NULL),
   158   _deallocate_list(NULL),
   158   _deallocate_list(NULL),
   162   if (!h_class_loader.is_null()) {
   162   if (!h_class_loader.is_null()) {
   163     _class_loader = _handles.add(h_class_loader());
   163     _class_loader = _handles.add(h_class_loader());
   164     _class_loader_klass = h_class_loader->klass();
   164     _class_loader_klass = h_class_loader->klass();
   165   }
   165   }
   166 
   166 
   167   if (!is_anonymous) {
   167   if (!is_unsafe_anonymous) {
   168     // The holder is initialized later for anonymous classes, and before calling anything
   168     // The holder is initialized later for unsafe anonymous classes, and before calling anything
   169     // that call class_loader().
   169     // that call class_loader().
   170     initialize_holder(h_class_loader);
   170     initialize_holder(h_class_loader);
   171 
   171 
   172     // A ClassLoaderData created solely for an anonymous class should never have a
   172     // A ClassLoaderData created solely for an unsafe anonymous class should never have a
   173     // ModuleEntryTable or PackageEntryTable created for it. The defining package
   173     // ModuleEntryTable or PackageEntryTable created for it. The defining package
   174     // and module for an anonymous class will be found in its host class.
   174     // and module for an unsafe anonymous class will be found in its host class.
   175     _packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size);
   175     _packages = new PackageEntryTable(PackageEntryTable::_packagetable_entry_size);
   176     if (h_class_loader.is_null()) {
   176     if (h_class_loader.is_null()) {
   177       // Create unnamed module for boot loader
   177       // Create unnamed module for boot loader
   178       _unnamed_module = ModuleEntry::create_boot_unnamed_module(this);
   178       _unnamed_module = ModuleEntry::create_boot_unnamed_module(this);
   179     } else {
   179     } else {
   285   }
   285   }
   286 
   286 
   287   return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0;
   287   return (int) Atomic::cmpxchg(1, &_claimed, 0) == 0;
   288 }
   288 }
   289 
   289 
   290 // Anonymous classes have their own ClassLoaderData that is marked to keep alive
   290 // Unsafe anonymous classes have their own ClassLoaderData that is marked to keep alive
   291 // while the class is being parsed, and if the class appears on the module fixup list.
   291 // while the class is being parsed, and if the class appears on the module fixup list.
   292 // Due to the uniqueness that no other class shares the anonymous class' name or
   292 // Due to the uniqueness that no other class shares the unsafe anonymous class' name or
   293 // ClassLoaderData, no other non-GC thread has knowledge of the anonymous class while
   293 // ClassLoaderData, no other non-GC thread has knowledge of the unsafe anonymous class while
   294 // it is being defined, therefore _keep_alive is not volatile or atomic.
   294 // it is being defined, therefore _keep_alive is not volatile or atomic.
   295 void ClassLoaderData::inc_keep_alive() {
   295 void ClassLoaderData::inc_keep_alive() {
   296   if (is_anonymous()) {
   296   if (is_unsafe_anonymous()) {
   297     assert(_keep_alive >= 0, "Invalid keep alive increment count");
   297     assert(_keep_alive >= 0, "Invalid keep alive increment count");
   298     _keep_alive++;
   298     _keep_alive++;
   299   }
   299   }
   300 }
   300 }
   301 
   301 
   302 void ClassLoaderData::dec_keep_alive() {
   302 void ClassLoaderData::dec_keep_alive() {
   303   if (is_anonymous()) {
   303   if (is_unsafe_anonymous()) {
   304     assert(_keep_alive > 0, "Invalid keep alive decrement count");
   304     assert(_keep_alive > 0, "Invalid keep alive decrement count");
   305     _keep_alive--;
   305     _keep_alive--;
   306   }
   306   }
   307 }
   307 }
   308 
   308 
   400   ClassLoaderData * const to_cld = k->class_loader_data();
   400   ClassLoaderData * const to_cld = k->class_loader_data();
   401 
   401 
   402   // Do not need to record dependency if the dependency is to a class whose
   402   // Do not need to record dependency if the dependency is to a class whose
   403   // class loader data is never freed.  (i.e. the dependency's class loader
   403   // class loader data is never freed.  (i.e. the dependency's class loader
   404   // is one of the three builtin class loaders and the dependency is not
   404   // is one of the three builtin class loaders and the dependency is not
   405   // anonymous.)
   405   // unsafe anonymous.)
   406   if (to_cld->is_permanent_class_loader_data()) {
   406   if (to_cld->is_permanent_class_loader_data()) {
   407     return;
   407     return;
   408   }
   408   }
   409 
   409 
   410   oop to;
   410   oop to;
   411   if (to_cld->is_anonymous()) {
   411   if (to_cld->is_unsafe_anonymous()) {
   412     // Just return if an anonymous class is attempting to record a dependency
   412     // Just return if an unsafe anonymous class is attempting to record a dependency
   413     // to itself.  (Note that every anonymous class has its own unique class
   413     // to itself.  (Note that every unsafe anonymous class has its own unique class
   414     // loader data.)
   414     // loader data.)
   415     if (to_cld == from_cld) {
   415     if (to_cld == from_cld) {
   416       return;
   416       return;
   417     }
   417     }
   418     // Anonymous class dependencies are through the mirror.
   418     // Unsafe anonymous class dependencies are through the mirror.
   419     to = k->java_mirror();
   419     to = k->java_mirror();
   420   } else {
   420   } else {
   421     to = to_cld->class_loader();
   421     to = to_cld->class_loader();
   422     oop from = from_cld->class_loader();
   422     oop from = from_cld->class_loader();
   423 
   423 
   638 
   638 
   639 const int _boot_loader_dictionary_size    = 1009;
   639 const int _boot_loader_dictionary_size    = 1009;
   640 const int _default_loader_dictionary_size = 107;
   640 const int _default_loader_dictionary_size = 107;
   641 
   641 
   642 Dictionary* ClassLoaderData::create_dictionary() {
   642 Dictionary* ClassLoaderData::create_dictionary() {
   643   assert(!is_anonymous(), "anonymous class loader data do not have a dictionary");
   643   assert(!is_unsafe_anonymous(), "unsafe anonymous class loader data do not have a dictionary");
   644   int size;
   644   int size;
   645   bool resizable = false;
   645   bool resizable = false;
   646   if (_the_null_class_loader_data == NULL) {
   646   if (_the_null_class_loader_data == NULL) {
   647     size = _boot_loader_dictionary_size;
   647     size = _boot_loader_dictionary_size;
   648     resizable = true;
   648     resizable = true;
   675   }
   675   }
   676 }
   676 }
   677 
   677 
   678 // Unloading support
   678 // Unloading support
   679 bool ClassLoaderData::is_alive() const {
   679 bool ClassLoaderData::is_alive() const {
   680   bool alive = keep_alive()         // null class loader and incomplete anonymous klasses.
   680   bool alive = keep_alive()         // null class loader and incomplete unsafe anonymous klasses.
   681       || (_holder.peek() != NULL);  // and not cleaned by the GC weak handle processing.
   681       || (_holder.peek() != NULL);  // and not cleaned by the GC weak handle processing.
   682 
   682 
   683   return alive;
   683   return alive;
   684 }
   684 }
   685 
   685 
   765   }
   765   }
   766 }
   766 }
   767 
   767 
   768 // Returns true if this class loader data is for the app class loader
   768 // Returns true if this class loader data is for the app class loader
   769 // or a user defined system class loader.  (Note that the class loader
   769 // or a user defined system class loader.  (Note that the class loader
   770 // data may be anonymous.)
   770 // data may be unsafe anonymous.)
   771 bool ClassLoaderData::is_system_class_loader_data() const {
   771 bool ClassLoaderData::is_system_class_loader_data() const {
   772   return SystemDictionary::is_system_class_loader(class_loader());
   772   return SystemDictionary::is_system_class_loader(class_loader());
   773 }
   773 }
   774 
   774 
   775 // Returns true if this class loader data is for the platform class loader.
   775 // Returns true if this class loader data is for the platform class loader.
   776 // (Note that the class loader data may be anonymous.)
   776 // (Note that the class loader data may be unsafe anonymous.)
   777 bool ClassLoaderData::is_platform_class_loader_data() const {
   777 bool ClassLoaderData::is_platform_class_loader_data() const {
   778   return SystemDictionary::is_platform_class_loader(class_loader());
   778   return SystemDictionary::is_platform_class_loader(class_loader());
   779 }
   779 }
   780 
   780 
   781 // Returns true if the class loader for this class loader data is one of
   781 // Returns true if the class loader for this class loader data is one of
   782 // the 3 builtin (boot application/system or platform) class loaders,
   782 // the 3 builtin (boot application/system or platform) class loaders,
   783 // including a user-defined system class loader.  Note that if the class
   783 // including a user-defined system class loader.  Note that if the class
   784 // loader data is for an anonymous class then it may get freed by a GC
   784 // loader data is for an unsafe anonymous class then it may get freed by a GC
   785 // even if its class loader is one of these loaders.
   785 // even if its class loader is one of these loaders.
   786 bool ClassLoaderData::is_builtin_class_loader_data() const {
   786 bool ClassLoaderData::is_builtin_class_loader_data() const {
   787   return (is_boot_class_loader_data() ||
   787   return (is_boot_class_loader_data() ||
   788           SystemDictionary::is_system_class_loader(class_loader()) ||
   788           SystemDictionary::is_system_class_loader(class_loader()) ||
   789           SystemDictionary::is_platform_class_loader(class_loader()));
   789           SystemDictionary::is_platform_class_loader(class_loader()));
   790 }
   790 }
   791 
   791 
   792 // Returns true if this class loader data is a class loader data
   792 // Returns true if this class loader data is a class loader data
   793 // that is not ever freed by a GC.  It must be one of the builtin
   793 // that is not ever freed by a GC.  It must be the CLD for one of the builtin
   794 // class loaders and not anonymous.
   794 // class loaders and not the CLD for an unsafe anonymous class.
   795 bool ClassLoaderData::is_permanent_class_loader_data() const {
   795 bool ClassLoaderData::is_permanent_class_loader_data() const {
   796   return is_builtin_class_loader_data() && !is_anonymous();
   796   return is_builtin_class_loader_data() && !is_unsafe_anonymous();
   797 }
   797 }
   798 
   798 
   799 ClassLoaderMetaspace* ClassLoaderData::metaspace_non_null() {
   799 ClassLoaderMetaspace* ClassLoaderData::metaspace_non_null() {
   800   // If the metaspace has not been allocated, create a new one.  Might want
   800   // If the metaspace has not been allocated, create a new one.  Might want
   801   // to create smaller arena for Reflection class loaders also.
   801   // to create smaller arena for Reflection class loaders also.
   808     // Check if _metaspace got allocated while we were waiting for this lock.
   808     // Check if _metaspace got allocated while we were waiting for this lock.
   809     if ((metaspace = _metaspace) == NULL) {
   809     if ((metaspace = _metaspace) == NULL) {
   810       if (this == the_null_class_loader_data()) {
   810       if (this == the_null_class_loader_data()) {
   811         assert (class_loader() == NULL, "Must be");
   811         assert (class_loader() == NULL, "Must be");
   812         metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::BootMetaspaceType);
   812         metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::BootMetaspaceType);
   813       } else if (is_anonymous()) {
   813       } else if (is_unsafe_anonymous()) {
   814         metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::AnonymousMetaspaceType);
   814         metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::UnsafeAnonymousMetaspaceType);
   815       } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
   815       } else if (class_loader()->is_a(SystemDictionary::reflect_DelegatingClassLoader_klass())) {
   816         metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType);
   816         metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::ReflectionMetaspaceType);
   817       } else {
   817       } else {
   818         metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::StandardMetaspaceType);
   818         metaspace = new ClassLoaderMetaspace(_metaspace_lock, Metaspace::StandardMetaspaceType);
   819       }
   819       }
   960       remove_class(ik);
   960       remove_class(ik);
   961     }
   961     }
   962   }
   962   }
   963 }
   963 }
   964 
   964 
   965 // These anonymous class loaders are to contain classes used for JSR292
   965 // These CLDs are to contain unsafe anonymous classes used for JSR292
   966 ClassLoaderData* ClassLoaderData::anonymous_class_loader_data(Handle loader) {
   966 ClassLoaderData* ClassLoaderData::unsafe_anonymous_class_loader_data(Handle loader) {
   967   // Add a new class loader data to the graph.
   967   // Add a new class loader data to the graph.
   968   return ClassLoaderDataGraph::add(loader, true);
   968   return ClassLoaderDataGraph::add(loader, true);
   969 }
   969 }
   970 
   970 
   971 // Caller needs ResourceMark
   971 // Caller needs ResourceMark
  1003     class_loader()->print_value_on(out);  // includes loader_name_and_id() and address of class loader instance
  1003     class_loader()->print_value_on(out);  // includes loader_name_and_id() and address of class loader instance
  1004   } else {
  1004   } else {
  1005     // loader data: 0xsomeaddr of 'bootstrap'
  1005     // loader data: 0xsomeaddr of 'bootstrap'
  1006     out->print("loader data: " INTPTR_FORMAT " of %s", p2i(this), loader_name_and_id());
  1006     out->print("loader data: " INTPTR_FORMAT " of %s", p2i(this), loader_name_and_id());
  1007   }
  1007   }
  1008   if (is_anonymous()) {
  1008   if (is_unsafe_anonymous()) {
  1009     out->print(" anonymous");
  1009     out->print(" unsafe anonymous");
  1010   }
  1010   }
  1011 }
  1011 }
  1012 
  1012 
  1013 #ifndef PRODUCT
  1013 #ifndef PRODUCT
  1014 void ClassLoaderData::print_on(outputStream* out) const {
  1014 void ClassLoaderData::print_on(outputStream* out) const {
  1015   out->print("ClassLoaderData CLD: " PTR_FORMAT ", loader: " PTR_FORMAT ", loader_klass: %s {",
  1015   out->print("ClassLoaderData CLD: " PTR_FORMAT ", loader: " PTR_FORMAT ", loader_klass: %s {",
  1016               p2i(this), p2i(_class_loader.ptr_raw()), loader_name_and_id());
  1016               p2i(this), p2i(_class_loader.ptr_raw()), loader_name_and_id());
  1017   if (is_anonymous()) out->print(" anonymous");
  1017   if (is_unsafe_anonymous()) out->print(" unsafe anonymous");
  1018   if (claimed()) out->print(" claimed");
  1018   if (claimed()) out->print(" claimed");
  1019   if (is_unloading()) out->print(" unloading");
  1019   if (is_unloading()) out->print(" unloading");
  1020   out->print(" metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null()));
  1020   out->print(" metaspace: " INTPTR_FORMAT, p2i(metaspace_or_null()));
  1021 
  1021 
  1022   if (_jmethod_ids != NULL) {
  1022   if (_jmethod_ids != NULL) {
  1030 
  1030 
  1031 void ClassLoaderData::verify() {
  1031 void ClassLoaderData::verify() {
  1032   assert_locked_or_safepoint(_metaspace_lock);
  1032   assert_locked_or_safepoint(_metaspace_lock);
  1033   oop cl = class_loader();
  1033   oop cl = class_loader();
  1034 
  1034 
  1035   guarantee(this == class_loader_data(cl) || is_anonymous(), "Must be the same");
  1035   guarantee(this == class_loader_data(cl) || is_unsafe_anonymous(), "Must be the same");
  1036   guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_anonymous(), "must be");
  1036   guarantee(cl != NULL || this == ClassLoaderData::the_null_class_loader_data() || is_unsafe_anonymous(), "must be");
  1037 
  1037 
  1038   // Verify the integrity of the allocated space.
  1038   // Verify the integrity of the allocated space.
  1039   if (metaspace_or_null() != NULL) {
  1039   if (metaspace_or_null() != NULL) {
  1040     metaspace_or_null()->verify();
  1040     metaspace_or_null()->verify();
  1041   }
  1041   }
  1067 bool ClassLoaderDataGraph::_safepoint_cleanup_needed = false;
  1067 bool ClassLoaderDataGraph::_safepoint_cleanup_needed = false;
  1068 bool ClassLoaderDataGraph::_metaspace_oom = false;
  1068 bool ClassLoaderDataGraph::_metaspace_oom = false;
  1069 
  1069 
  1070 // Add a new class loader data node to the list.  Assign the newly created
  1070 // Add a new class loader data node to the list.  Assign the newly created
  1071 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
  1071 // ClassLoaderData into the java/lang/ClassLoader object as a hidden field
  1072 ClassLoaderData* ClassLoaderDataGraph::add_to_graph(Handle loader, bool is_anonymous) {
  1072 ClassLoaderData* ClassLoaderDataGraph::add_to_graph(Handle loader, bool is_unsafe_anonymous) {
  1073   NoSafepointVerifier no_safepoints; // we mustn't GC until we've installed the
  1073   NoSafepointVerifier no_safepoints; // we mustn't GC until we've installed the
  1074                                      // ClassLoaderData in the graph since the CLD
  1074                                      // ClassLoaderData in the graph since the CLD
  1075                                      // contains oops in _handles that must be walked.
  1075                                      // contains oops in _handles that must be walked.
  1076 
  1076 
  1077   ClassLoaderData* cld = new ClassLoaderData(loader, is_anonymous);
  1077   ClassLoaderData* cld = new ClassLoaderData(loader, is_unsafe_anonymous);
  1078 
  1078 
  1079   if (!is_anonymous) {
  1079   if (!is_unsafe_anonymous) {
  1080     // First, Atomically set it
  1080     // First, Atomically set it
  1081     ClassLoaderData* old = java_lang_ClassLoader::cmpxchg_loader_data(cld, loader(), NULL);
  1081     ClassLoaderData* old = java_lang_ClassLoader::cmpxchg_loader_data(cld, loader(), NULL);
  1082     if (old != NULL) {
  1082     if (old != NULL) {
  1083       delete cld;
  1083       delete cld;
  1084       // Returns the data.
  1084       // Returns the data.
  1107     }
  1107     }
  1108     next = exchanged;
  1108     next = exchanged;
  1109   } while (true);
  1109   } while (true);
  1110 }
  1110 }
  1111 
  1111 
  1112 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_anonymous) {
  1112 ClassLoaderData* ClassLoaderDataGraph::add(Handle loader, bool is_unsafe_anonymous) {
  1113   ClassLoaderData* loader_data = add_to_graph(loader, is_anonymous);
  1113   ClassLoaderData* loader_data = add_to_graph(loader, is_unsafe_anonymous);
  1114   // Initialize _name and _name_and_id after the loader data is added to the
  1114   // Initialize _name and _name_and_id after the loader data is added to the
  1115   // CLDG because adding the Symbol for _name and _name_and_id might safepoint.
  1115   // CLDG because adding the Symbol for _name and _name_and_id might safepoint.
  1116   if (loader.not_null()) {
  1116   if (loader.not_null()) {
  1117     loader_data->initialize_name(loader);
  1117     loader_data->initialize_name(loader);
  1118   }
  1118   }