src/share/vm/classfile/systemDictionary.cpp

Print this page
rev 4773 : 8005849: JEP 167: Event-Based JVM Tracing
Reviewed-by: acorn, coleenp, sla
Contributed-by: Karen Kinnear <karen.kinnear@oracle.com>, Bengt Rutisson <bengt.rutisson@oracle.com>, Calvin Cheung <calvin.cheung@oracle.com>, Erik Gahlin <erik.gahlin@oracle.com>, Erik Helin <erik.helin@oracle.com>, Jesper Wilhelmsson <jesper.wilhelmsson@oracle.com>, Keith McGuigan <keith.mcguigan@oracle.com>, Mattias Tobiasson <mattias.tobiasson@oracle.com>, Markus Gronlund <markus.gronlund@oracle.com>, Mikael Auno <mikael.auno@oracle.com>, Nils Eliasson <nils.eliasson@oracle.com>, Nils Loodin <nils.loodin@oracle.com>, Rickard Backman <rickard.backman@oracle.com>, Staffan Larsen <staffan.larsen@oracle.com>, Stefan Karlsson <stefan.karlsson@oracle.com>, Yekaterina Kantserova <yekaterina.kantserova@oracle.com>


  39 #include "oops/instanceKlass.hpp"
  40 #include "oops/instanceRefKlass.hpp"
  41 #include "oops/klass.inline.hpp"
  42 #include "oops/methodData.hpp"
  43 #include "oops/objArrayKlass.hpp"
  44 #include "oops/oop.inline.hpp"
  45 #include "oops/oop.inline2.hpp"
  46 #include "oops/typeArrayKlass.hpp"
  47 #include "prims/jvmtiEnvBase.hpp"
  48 #include "prims/methodHandles.hpp"
  49 #include "runtime/biasedLocking.hpp"
  50 #include "runtime/fieldType.hpp"
  51 #include "runtime/handles.inline.hpp"
  52 #include "runtime/java.hpp"
  53 #include "runtime/javaCalls.hpp"
  54 #include "runtime/mutexLocker.hpp"
  55 #include "runtime/signature.hpp"
  56 #include "services/classLoadingService.hpp"
  57 #include "services/threadService.hpp"
  58 





  59 
  60 Dictionary*            SystemDictionary::_dictionary          = NULL;
  61 PlaceholderTable*      SystemDictionary::_placeholders        = NULL;
  62 Dictionary*            SystemDictionary::_shared_dictionary   = NULL;
  63 LoaderConstraintTable* SystemDictionary::_loader_constraints  = NULL;
  64 ResolutionErrorTable*  SystemDictionary::_resolution_errors   = NULL;
  65 SymbolPropertyTable*   SystemDictionary::_invoke_method_table = NULL;
  66 
  67 
  68 int         SystemDictionary::_number_of_modifications = 0;
  69 int         SystemDictionary::_sdgeneration               = 0;
  70 const int   SystemDictionary::_primelist[_prime_array_size] = {1009,2017,4049,5051,10103,
  71               20201,40423,99991};
  72 
  73 oop         SystemDictionary::_system_loader_lock_obj     =  NULL;
  74 
  75 Klass*      SystemDictionary::_well_known_klasses[SystemDictionary::WKID_LIMIT]
  76                                                           =  { NULL /*, NULL...*/ };
  77 
  78 Klass*      SystemDictionary::_box_klasses[T_VOID+1]      =  { NULL /*, NULL...*/ };


 569         // all the superclass/superinterface loading it can, until
 570         // the original thread completes the class loading or fails
 571         // If it completes we will use the resulting InstanceKlass
 572         // which we will find below in the systemDictionary.
 573         // We also get here for parallel bootstrap classloader
 574         if (class_loader.is_null()) {
 575           SystemDictionary_lock->wait();
 576         } else {
 577           double_lock_wait(lockObject, THREAD);
 578         }
 579       } else {
 580         // If not in SD and not in PH, other thread's load must have failed
 581         super_load_in_progress = false;
 582       }
 583     }
 584   }
 585   return (nh);
 586 }
 587 
 588 
 589 Klass* SystemDictionary::resolve_instance_class_or_null(Symbol* name, Handle class_loader, Handle protection_domain, TRAPS) {



 590   assert(name != NULL && !FieldType::is_array(name) &&
 591          !FieldType::is_obj(name), "invalid class name");
 592 


 593   // UseNewReflection
 594   // Fix for 4474172; see evaluation for more details
 595   class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
 596   ClassLoaderData *loader_data = register_loader(class_loader, CHECK_NULL);
 597 
 598   // Do lookup to see if class already exist and the protection domain
 599   // has the right access
 600   // This call uses find which checks protection domain already matches
 601   // All subsequent calls use find_class, and set has_loaded_class so that
 602   // before we return a result we call out to java to check for valid protection domain
 603   // to allow returning the Klass* and add it to the pd_set if it is valid
 604   unsigned int d_hash = dictionary()->compute_hash(name, loader_data);
 605   int d_index = dictionary()->hash_to_index(d_hash);
 606   Klass* probe = dictionary()->find(d_index, d_hash, name, loader_data,
 607                                       protection_domain, THREAD);
 608   if (probe != NULL) return probe;
 609 
 610 
 611   // Non-bootstrap class loaders will call out to class loader and
 612   // define via jvm/jni_DefineClass which will acquire the


 789           }
 790         }
 791       }
 792 
 793       // If everything was OK (no exceptions, no null return value), and
 794       // class_loader is NOT the defining loader, do a little more bookkeeping.
 795       if (!HAS_PENDING_EXCEPTION && !k.is_null() &&
 796         k->class_loader() != class_loader()) {
 797 
 798         check_constraints(d_index, d_hash, k, class_loader, false, THREAD);
 799 
 800         // Need to check for a PENDING_EXCEPTION again; check_constraints
 801         // can throw and doesn't use the CHECK macro.
 802         if (!HAS_PENDING_EXCEPTION) {
 803           { // Grabbing the Compile_lock prevents systemDictionary updates
 804             // during compilations.
 805             MutexLocker mu(Compile_lock, THREAD);
 806             update_dictionary(d_index, d_hash, p_index, p_hash,
 807                             k, class_loader, THREAD);
 808           }

 809           if (JvmtiExport::should_post_class_load()) {
 810             Thread *thread = THREAD;
 811             assert(thread->is_Java_thread(), "thread->is_Java_thread()");
 812             JvmtiExport::post_class_load((JavaThread *) thread, k());
 813           }
 814         }
 815       }
 816     } // load_instance_class loop
 817 
 818     if (HAS_PENDING_EXCEPTION) {
 819       // An exception, such as OOM could have happened at various places inside
 820       // load_instance_class. We might have partially initialized a shared class
 821       // and need to clean it up.
 822       if (class_loader.is_null()) {
 823         // In some cases k may be null. Let's find the shared class again.
 824         instanceKlassHandle ik(THREAD, find_shared_class(name));
 825         if (ik.not_null()) {
 826           if (ik->class_loader_data() == NULL) {
 827             // We didn't go as far as Klass::restore_unshareable_info(),
 828             // so nothing to clean up.


 853             }
 854           }
 855         }
 856       }
 857     }
 858 
 859     if (load_instance_added == true) {
 860       // clean up placeholder entries for LOAD_INSTANCE success or error
 861       // This brackets the SystemDictionary updates for both defining
 862       // and initiating loaders
 863       MutexLocker mu(SystemDictionary_lock, THREAD);
 864         placeholders()->find_and_remove(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, THREAD);
 865         SystemDictionary_lock->notify_all();
 866     }
 867   }
 868 
 869   if (HAS_PENDING_EXCEPTION || k.is_null()) {
 870     return NULL;
 871   }
 872 


 873 #ifdef ASSERT
 874   {
 875     ClassLoaderData* loader_data = k->class_loader_data();
 876     MutexLocker mu(SystemDictionary_lock, THREAD);
 877     Klass* kk = find_class(name, loader_data);
 878     assert(kk == k(), "should be present in dictionary");
 879   }
 880 #endif
 881 
 882   // return if the protection domain in NULL
 883   if (protection_domain() == NULL) return k();
 884 
 885   // Check the protection domain has the right access
 886   {
 887     MutexLocker mu(SystemDictionary_lock, THREAD);
 888     // Note that we have an entry, and entries can be deleted only during GC,
 889     // so we cannot allow GC to occur while we're holding this entry.
 890     // We're using a No_Safepoint_Verifier to catch any place where we
 891     // might potentially do a GC at all.
 892     // Dictionary::do_unloading() asserts that classes in SD are only


 976       k = k->array_klass_or_null(fd.dimension());
 977     }
 978   } else {
 979     k = find(class_name, class_loader, protection_domain, THREAD);
 980   }
 981   return k;
 982 }
 983 
 984 // Note: this method is much like resolve_from_stream, but
 985 // updates no supplemental data structures.
 986 // TODO consolidate the two methods with a helper routine?
 987 Klass* SystemDictionary::parse_stream(Symbol* class_name,
 988                                       Handle class_loader,
 989                                       Handle protection_domain,
 990                                       ClassFileStream* st,
 991                                       KlassHandle host_klass,
 992                                       GrowableArray<Handle>* cp_patches,
 993                                       TRAPS) {
 994   TempNewSymbol parsed_name = NULL;
 995 


 996   ClassLoaderData* loader_data;
 997   if (host_klass.not_null()) {
 998     // Create a new CLD for anonymous class, that uses the same class loader
 999     // as the host_klass
1000     assert(EnableInvokeDynamic, "");
1001     guarantee(host_klass->class_loader() == class_loader(), "should be the same");
1002     loader_data = ClassLoaderData::anonymous_class_loader_data(class_loader(), CHECK_NULL);
1003     loader_data->record_dependency(host_klass(), CHECK_NULL);
1004   } else {
1005     loader_data = ClassLoaderData::class_loader_data(class_loader());
1006   }
1007 
1008   // Parse the stream. Note that we do this even though this klass might
1009   // already be present in the SystemDictionary, otherwise we would not
1010   // throw potential ClassFormatErrors.
1011   //
1012   // Note: "name" is updated.
1013 
1014   instanceKlassHandle k = ClassFileParser(st).parseClassFile(class_name,
1015                                                              loader_data,


1031 
1032       // Add to class hierarchy, initialize vtables, and do possible
1033       // deoptimizations.
1034       add_to_hierarchy(k, CHECK_NULL); // No exception, but can block
1035 
1036       // But, do not add to system dictionary.
1037     }
1038 
1039     // Rewrite and patch constant pool here.
1040     k->link_class(CHECK_NULL);
1041     if (cp_patches != NULL) {
1042       k->constants()->patch_resolved_references(cp_patches);
1043     }
1044     k->eager_initialize(CHECK_NULL);
1045 
1046     // notify jvmti
1047     if (JvmtiExport::should_post_class_load()) {
1048         assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1049         JvmtiExport::post_class_load((JavaThread *) THREAD, k());
1050     }


1051   }
1052   assert(host_klass.not_null() || cp_patches == NULL,
1053          "cp_patches only found with host_klass");
1054 
1055   return k();
1056 }
1057 
1058 // Add a klass to the system from a stream (called by jni_DefineClass and
1059 // JVM_DefineClass).
1060 // Note: class_name can be NULL. In that case we do not know the name of
1061 // the class until we have parsed the stream.
1062 
1063 Klass* SystemDictionary::resolve_from_stream(Symbol* class_name,
1064                                              Handle class_loader,
1065                                              Handle protection_domain,
1066                                              ClassFileStream* st,
1067                                              bool verify,
1068                                              TRAPS) {
1069 
1070   // Classloaders that support parallelism, e.g. bootstrap classloader,


1418 
1419     MutexLocker mu_r(Compile_lock, THREAD);
1420 
1421     // Add to class hierarchy, initialize vtables, and do possible
1422     // deoptimizations.
1423     add_to_hierarchy(k, CHECK); // No exception, but can block
1424 
1425     // Add to systemDictionary - so other classes can see it.
1426     // Grabs and releases SystemDictionary_lock
1427     update_dictionary(d_index, d_hash, p_index, p_hash,
1428                       k, class_loader_h, THREAD);
1429   }
1430   k->eager_initialize(THREAD);
1431 
1432   // notify jvmti
1433   if (JvmtiExport::should_post_class_load()) {
1434       assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1435       JvmtiExport::post_class_load((JavaThread *) THREAD, k());
1436 
1437   }

1438 }
1439 
1440 // Support parallel classloading
1441 // All parallel class loaders, including bootstrap classloader
1442 // lock a placeholder entry for this class/class_loader pair
1443 // to allow parallel defines of different classes for this class loader
1444 // With AllowParallelDefine flag==true, in case they do not synchronize around
1445 // FindLoadedClass/DefineClass, calls, we check for parallel
1446 // loading for them, wait if a defineClass is in progress
1447 // and return the initial requestor's results
1448 // This flag does not apply to the bootstrap classloader.
1449 // With AllowParallelDefine flag==false, call through to define_instance_class
1450 // which will throw LinkageError: duplicate class definition.
1451 // False is the requested default.
1452 // For better performance, the class loaders should synchronize
1453 // findClass(), i.e. FindLoadedClass/DefineClassIfAbsent or they
1454 // potentially waste time reading and parsing the bytestream.
1455 // Note: VM callers should ensure consistency of k/class_name,class_loader
1456 instanceKlassHandle SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader, instanceKlassHandle k, TRAPS) {
1457 


1661 
1662   // Placeholders. These represent classes we're actively loading.
1663   placeholders()->classes_do(closure);
1664 }
1665 
1666 // Calculate a "good" systemdictionary size based
1667 // on predicted or current loaded classes count
1668 int SystemDictionary::calculate_systemdictionary_size(int classcount) {
1669   int newsize = _old_default_sdsize;
1670   if ((classcount > 0)  && !DumpSharedSpaces) {
1671     int desiredsize = classcount/_average_depth_goal;
1672     for (newsize = _primelist[_sdgeneration]; _sdgeneration < _prime_array_size -1;
1673          newsize = _primelist[++_sdgeneration]) {
1674       if (desiredsize <=  newsize) {
1675         break;
1676       }
1677     }
1678   }
1679   return newsize;
1680 }

1681 // Assumes classes in the SystemDictionary are only unloaded at a safepoint
1682 // Note: anonymous classes are not in the SD.
1683 bool SystemDictionary::do_unloading(BoolObjectClosure* is_alive) {
1684   // First, mark for unload all ClassLoaderData referencing a dead class loader.
1685   bool has_dead_loaders = ClassLoaderDataGraph::do_unloading(is_alive);
1686   bool unloading_occurred = false;
1687   if (has_dead_loaders) {
1688     unloading_occurred = dictionary()->do_unloading();
1689     constraints()->purge_loader_constraints();
1690     resolution_errors()->purge_resolution_errors();
1691 }
1692   return unloading_occurred;
1693 }
1694 
1695 void SystemDictionary::oops_do(OopClosure* f) {
1696   f->do_oop(&_java_system_loader);
1697   f->do_oop(&_system_loader_lock_obj);
1698 
1699   // Adjust dictionary
1700   dictionary()->oops_do(f);


2007   // See whether biased locking is enabled and if so set it for this
2008   // klass.
2009   // Note that this must be done past the last potential blocking
2010   // point / safepoint. We enable biased locking lazily using a
2011   // VM_Operation to iterate the SystemDictionary and installing the
2012   // biasable mark word into each InstanceKlass's prototype header.
2013   // To avoid race conditions where we accidentally miss enabling the
2014   // optimization for one class in the process of being added to the
2015   // dictionary, we must not safepoint after the test of
2016   // BiasedLocking::enabled().
2017   if (UseBiasedLocking && BiasedLocking::enabled()) {
2018     // Set biased locking bit for all loaded classes; it will be
2019     // cleared if revocation occurs too often for this type
2020     // NOTE that we must only do this when the class is initally
2021     // defined, not each time it is referenced from a new class loader
2022     if (k->class_loader() == class_loader()) {
2023       k->set_prototype_header(markOopDesc::biased_locking_prototype());
2024     }
2025   }
2026 
2027   // Assign a classid if one has not already been assigned.  The
2028   // counter does not need to be atomically incremented since this
2029   // is only done while holding the SystemDictionary_lock.
2030   // All loaded classes get a unique ID.
2031   TRACE_INIT_ID(k);
2032 
2033   // Make a new system dictionary entry.
2034   Klass* sd_check = find_class(d_index, d_hash, name, loader_data);
2035   if (sd_check == NULL) {
2036     dictionary()->add_klass(name, loader_data, k);
2037     notice_modification();
2038   }
2039 #ifdef ASSERT
2040   sd_check = find_class(d_index, d_hash, name, loader_data);
2041   assert (sd_check != NULL, "should have entry in system dictionary");
2042   // Note: there may be a placeholder entry: for circularity testing
2043   // or for parallel defines
2044 #endif
2045     SystemDictionary_lock->notify_all();
2046   }
2047 }
2048 
2049 
2050 // Try to find a class name using the loader constraints.  The
2051 // loader constraints might know about a class that isn't fully loaded
2052 // yet and these will be ignored.


2595   constraints()->verify(dictionary(), placeholders());
2596 }
2597 
2598 
2599 void SystemDictionary::verify_obj_klass_present(Symbol* class_name,
2600                                                 ClassLoaderData* loader_data) {
2601   GCMutexLocker mu(SystemDictionary_lock);
2602   Symbol* name;
2603 
2604   Klass* probe = find_class(class_name, loader_data);
2605   if (probe == NULL) {
2606     probe = SystemDictionary::find_shared_class(class_name);
2607     if (probe == NULL) {
2608       name = find_placeholder(class_name, loader_data);
2609     }
2610   }
2611   guarantee(probe != NULL || name != NULL,
2612             "Loaded klasses should be in SystemDictionary");
2613 }
2614 





















2615 #ifndef PRODUCT
2616 
2617 // statistics code
2618 class ClassStatistics: AllStatic {
2619  private:
2620   static int nclasses;        // number of classes
2621   static int nmethods;        // number of methods
2622   static int nmethoddata;     // number of methodData
2623   static int class_size;      // size of class objects in words
2624   static int method_size;     // size of method objects in words
2625   static int debug_size;      // size of debug info in methods
2626   static int methoddata_size; // size of methodData objects in words
2627 
2628   static void do_class(Klass* k) {
2629     nclasses++;
2630     class_size += k->size();
2631     if (k->oop_is_instance()) {
2632       InstanceKlass* ik = (InstanceKlass*)k;
2633       class_size += ik->methods()->size();
2634       class_size += ik->constants()->size();




  39 #include "oops/instanceKlass.hpp"
  40 #include "oops/instanceRefKlass.hpp"
  41 #include "oops/klass.inline.hpp"
  42 #include "oops/methodData.hpp"
  43 #include "oops/objArrayKlass.hpp"
  44 #include "oops/oop.inline.hpp"
  45 #include "oops/oop.inline2.hpp"
  46 #include "oops/typeArrayKlass.hpp"
  47 #include "prims/jvmtiEnvBase.hpp"
  48 #include "prims/methodHandles.hpp"
  49 #include "runtime/biasedLocking.hpp"
  50 #include "runtime/fieldType.hpp"
  51 #include "runtime/handles.inline.hpp"
  52 #include "runtime/java.hpp"
  53 #include "runtime/javaCalls.hpp"
  54 #include "runtime/mutexLocker.hpp"
  55 #include "runtime/signature.hpp"
  56 #include "services/classLoadingService.hpp"
  57 #include "services/threadService.hpp"
  58 
  59 #if INCLUDE_TRACE
  60  #include "trace/tracing.hpp"
  61  #include "trace/traceMacros.hpp"
  62 #endif
  63 
  64 
  65 Dictionary*            SystemDictionary::_dictionary          = NULL;
  66 PlaceholderTable*      SystemDictionary::_placeholders        = NULL;
  67 Dictionary*            SystemDictionary::_shared_dictionary   = NULL;
  68 LoaderConstraintTable* SystemDictionary::_loader_constraints  = NULL;
  69 ResolutionErrorTable*  SystemDictionary::_resolution_errors   = NULL;
  70 SymbolPropertyTable*   SystemDictionary::_invoke_method_table = NULL;
  71 
  72 
  73 int         SystemDictionary::_number_of_modifications = 0;
  74 int         SystemDictionary::_sdgeneration               = 0;
  75 const int   SystemDictionary::_primelist[_prime_array_size] = {1009,2017,4049,5051,10103,
  76               20201,40423,99991};
  77 
  78 oop         SystemDictionary::_system_loader_lock_obj     =  NULL;
  79 
  80 Klass*      SystemDictionary::_well_known_klasses[SystemDictionary::WKID_LIMIT]
  81                                                           =  { NULL /*, NULL...*/ };
  82 
  83 Klass*      SystemDictionary::_box_klasses[T_VOID+1]      =  { NULL /*, NULL...*/ };


 574         // all the superclass/superinterface loading it can, until
 575         // the original thread completes the class loading or fails
 576         // If it completes we will use the resulting InstanceKlass
 577         // which we will find below in the systemDictionary.
 578         // We also get here for parallel bootstrap classloader
 579         if (class_loader.is_null()) {
 580           SystemDictionary_lock->wait();
 581         } else {
 582           double_lock_wait(lockObject, THREAD);
 583         }
 584       } else {
 585         // If not in SD and not in PH, other thread's load must have failed
 586         super_load_in_progress = false;
 587       }
 588     }
 589   }
 590   return (nh);
 591 }
 592 
 593 
 594 Klass* SystemDictionary::resolve_instance_class_or_null(Symbol* name,
 595                                                         Handle class_loader,
 596                                                         Handle protection_domain,
 597                                                         TRAPS) {
 598   assert(name != NULL && !FieldType::is_array(name) &&
 599          !FieldType::is_obj(name), "invalid class name");
 600 
 601   TracingTime class_load_start_time = Tracing::time();
 602 
 603   // UseNewReflection
 604   // Fix for 4474172; see evaluation for more details
 605   class_loader = Handle(THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
 606   ClassLoaderData *loader_data = register_loader(class_loader, CHECK_NULL);
 607 
 608   // Do lookup to see if class already exist and the protection domain
 609   // has the right access
 610   // This call uses find which checks protection domain already matches
 611   // All subsequent calls use find_class, and set has_loaded_class so that
 612   // before we return a result we call out to java to check for valid protection domain
 613   // to allow returning the Klass* and add it to the pd_set if it is valid
 614   unsigned int d_hash = dictionary()->compute_hash(name, loader_data);
 615   int d_index = dictionary()->hash_to_index(d_hash);
 616   Klass* probe = dictionary()->find(d_index, d_hash, name, loader_data,
 617                                       protection_domain, THREAD);
 618   if (probe != NULL) return probe;
 619 
 620 
 621   // Non-bootstrap class loaders will call out to class loader and
 622   // define via jvm/jni_DefineClass which will acquire the


 799           }
 800         }
 801       }
 802 
 803       // If everything was OK (no exceptions, no null return value), and
 804       // class_loader is NOT the defining loader, do a little more bookkeeping.
 805       if (!HAS_PENDING_EXCEPTION && !k.is_null() &&
 806         k->class_loader() != class_loader()) {
 807 
 808         check_constraints(d_index, d_hash, k, class_loader, false, THREAD);
 809 
 810         // Need to check for a PENDING_EXCEPTION again; check_constraints
 811         // can throw and doesn't use the CHECK macro.
 812         if (!HAS_PENDING_EXCEPTION) {
 813           { // Grabbing the Compile_lock prevents systemDictionary updates
 814             // during compilations.
 815             MutexLocker mu(Compile_lock, THREAD);
 816             update_dictionary(d_index, d_hash, p_index, p_hash,
 817                               k, class_loader, THREAD);
 818           }
 819 
 820           if (JvmtiExport::should_post_class_load()) {
 821             Thread *thread = THREAD;
 822             assert(thread->is_Java_thread(), "thread->is_Java_thread()");
 823             JvmtiExport::post_class_load((JavaThread *) thread, k());
 824           }
 825         }
 826       }
 827     } // load_instance_class loop
 828 
 829     if (HAS_PENDING_EXCEPTION) {
 830       // An exception, such as OOM could have happened at various places inside
 831       // load_instance_class. We might have partially initialized a shared class
 832       // and need to clean it up.
 833       if (class_loader.is_null()) {
 834         // In some cases k may be null. Let's find the shared class again.
 835         instanceKlassHandle ik(THREAD, find_shared_class(name));
 836         if (ik.not_null()) {
 837           if (ik->class_loader_data() == NULL) {
 838             // We didn't go as far as Klass::restore_unshareable_info(),
 839             // so nothing to clean up.


 864             }
 865           }
 866         }
 867       }
 868     }
 869 
 870     if (load_instance_added == true) {
 871       // clean up placeholder entries for LOAD_INSTANCE success or error
 872       // This brackets the SystemDictionary updates for both defining
 873       // and initiating loaders
 874       MutexLocker mu(SystemDictionary_lock, THREAD);
 875       placeholders()->find_and_remove(p_index, p_hash, name, loader_data, PlaceholderTable::LOAD_INSTANCE, THREAD);
 876       SystemDictionary_lock->notify_all();
 877     }
 878   }
 879 
 880   if (HAS_PENDING_EXCEPTION || k.is_null()) {
 881     return NULL;
 882   }
 883 
 884   post_class_load_event(class_load_start_time, k, class_loader);
 885 
 886 #ifdef ASSERT
 887   {
 888     ClassLoaderData* loader_data = k->class_loader_data();
 889     MutexLocker mu(SystemDictionary_lock, THREAD);
 890     Klass* kk = find_class(name, loader_data);
 891     assert(kk == k(), "should be present in dictionary");
 892   }
 893 #endif
 894 
 895   // return if the protection domain in NULL
 896   if (protection_domain() == NULL) return k();
 897 
 898   // Check the protection domain has the right access
 899   {
 900     MutexLocker mu(SystemDictionary_lock, THREAD);
 901     // Note that we have an entry, and entries can be deleted only during GC,
 902     // so we cannot allow GC to occur while we're holding this entry.
 903     // We're using a No_Safepoint_Verifier to catch any place where we
 904     // might potentially do a GC at all.
 905     // Dictionary::do_unloading() asserts that classes in SD are only


 989       k = k->array_klass_or_null(fd.dimension());
 990     }
 991   } else {
 992     k = find(class_name, class_loader, protection_domain, THREAD);
 993   }
 994   return k;
 995 }
 996 
 997 // Note: this method is much like resolve_from_stream, but
 998 // updates no supplemental data structures.
 999 // TODO consolidate the two methods with a helper routine?
1000 Klass* SystemDictionary::parse_stream(Symbol* class_name,
1001                                       Handle class_loader,
1002                                       Handle protection_domain,
1003                                       ClassFileStream* st,
1004                                       KlassHandle host_klass,
1005                                       GrowableArray<Handle>* cp_patches,
1006                                       TRAPS) {
1007   TempNewSymbol parsed_name = NULL;
1008 
1009   TracingTime class_load_start_time = Tracing::time();
1010 
1011   ClassLoaderData* loader_data;
1012   if (host_klass.not_null()) {
1013     // Create a new CLD for anonymous class, that uses the same class loader
1014     // as the host_klass
1015     assert(EnableInvokeDynamic, "");
1016     guarantee(host_klass->class_loader() == class_loader(), "should be the same");
1017     loader_data = ClassLoaderData::anonymous_class_loader_data(class_loader(), CHECK_NULL);
1018     loader_data->record_dependency(host_klass(), CHECK_NULL);
1019   } else {
1020     loader_data = ClassLoaderData::class_loader_data(class_loader());
1021   }
1022 
1023   // Parse the stream. Note that we do this even though this klass might
1024   // already be present in the SystemDictionary, otherwise we would not
1025   // throw potential ClassFormatErrors.
1026   //
1027   // Note: "name" is updated.
1028 
1029   instanceKlassHandle k = ClassFileParser(st).parseClassFile(class_name,
1030                                                              loader_data,


1046 
1047       // Add to class hierarchy, initialize vtables, and do possible
1048       // deoptimizations.
1049       add_to_hierarchy(k, CHECK_NULL); // No exception, but can block
1050 
1051       // But, do not add to system dictionary.
1052     }
1053 
1054     // Rewrite and patch constant pool here.
1055     k->link_class(CHECK_NULL);
1056     if (cp_patches != NULL) {
1057       k->constants()->patch_resolved_references(cp_patches);
1058     }
1059     k->eager_initialize(CHECK_NULL);
1060 
1061     // notify jvmti
1062     if (JvmtiExport::should_post_class_load()) {
1063         assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1064         JvmtiExport::post_class_load((JavaThread *) THREAD, k());
1065     }
1066 
1067     post_class_load_event(class_load_start_time, k, class_loader);
1068   }
1069   assert(host_klass.not_null() || cp_patches == NULL,
1070          "cp_patches only found with host_klass");
1071 
1072   return k();
1073 }
1074 
1075 // Add a klass to the system from a stream (called by jni_DefineClass and
1076 // JVM_DefineClass).
1077 // Note: class_name can be NULL. In that case we do not know the name of
1078 // the class until we have parsed the stream.
1079 
1080 Klass* SystemDictionary::resolve_from_stream(Symbol* class_name,
1081                                              Handle class_loader,
1082                                              Handle protection_domain,
1083                                              ClassFileStream* st,
1084                                              bool verify,
1085                                              TRAPS) {
1086 
1087   // Classloaders that support parallelism, e.g. bootstrap classloader,


1435 
1436     MutexLocker mu_r(Compile_lock, THREAD);
1437 
1438     // Add to class hierarchy, initialize vtables, and do possible
1439     // deoptimizations.
1440     add_to_hierarchy(k, CHECK); // No exception, but can block
1441 
1442     // Add to systemDictionary - so other classes can see it.
1443     // Grabs and releases SystemDictionary_lock
1444     update_dictionary(d_index, d_hash, p_index, p_hash,
1445                       k, class_loader_h, THREAD);
1446   }
1447   k->eager_initialize(THREAD);
1448 
1449   // notify jvmti
1450   if (JvmtiExport::should_post_class_load()) {
1451       assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1452       JvmtiExport::post_class_load((JavaThread *) THREAD, k());
1453 
1454   }
1455 
1456 }
1457 
1458 // Support parallel classloading
1459 // All parallel class loaders, including bootstrap classloader
1460 // lock a placeholder entry for this class/class_loader pair
1461 // to allow parallel defines of different classes for this class loader
1462 // With AllowParallelDefine flag==true, in case they do not synchronize around
1463 // FindLoadedClass/DefineClass, calls, we check for parallel
1464 // loading for them, wait if a defineClass is in progress
1465 // and return the initial requestor's results
1466 // This flag does not apply to the bootstrap classloader.
1467 // With AllowParallelDefine flag==false, call through to define_instance_class
1468 // which will throw LinkageError: duplicate class definition.
1469 // False is the requested default.
1470 // For better performance, the class loaders should synchronize
1471 // findClass(), i.e. FindLoadedClass/DefineClassIfAbsent or they
1472 // potentially waste time reading and parsing the bytestream.
1473 // Note: VM callers should ensure consistency of k/class_name,class_loader
1474 instanceKlassHandle SystemDictionary::find_or_define_instance_class(Symbol* class_name, Handle class_loader, instanceKlassHandle k, TRAPS) {
1475 


1679 
1680   // Placeholders. These represent classes we're actively loading.
1681   placeholders()->classes_do(closure);
1682 }
1683 
1684 // Calculate a "good" systemdictionary size based
1685 // on predicted or current loaded classes count
1686 int SystemDictionary::calculate_systemdictionary_size(int classcount) {
1687   int newsize = _old_default_sdsize;
1688   if ((classcount > 0)  && !DumpSharedSpaces) {
1689     int desiredsize = classcount/_average_depth_goal;
1690     for (newsize = _primelist[_sdgeneration]; _sdgeneration < _prime_array_size -1;
1691          newsize = _primelist[++_sdgeneration]) {
1692       if (desiredsize <=  newsize) {
1693         break;
1694       }
1695     }
1696   }
1697   return newsize;
1698 }
1699 
1700 // Assumes classes in the SystemDictionary are only unloaded at a safepoint
1701 // Note: anonymous classes are not in the SD.
1702 bool SystemDictionary::do_unloading(BoolObjectClosure* is_alive) {
1703   // First, mark for unload all ClassLoaderData referencing a dead class loader.
1704   bool has_dead_loaders = ClassLoaderDataGraph::do_unloading(is_alive);
1705   bool unloading_occurred = false;
1706   if (has_dead_loaders) {
1707     unloading_occurred = dictionary()->do_unloading();
1708     constraints()->purge_loader_constraints();
1709     resolution_errors()->purge_resolution_errors();
1710 }
1711   return unloading_occurred;
1712 }
1713 
1714 void SystemDictionary::oops_do(OopClosure* f) {
1715   f->do_oop(&_java_system_loader);
1716   f->do_oop(&_system_loader_lock_obj);
1717 
1718   // Adjust dictionary
1719   dictionary()->oops_do(f);


2026   // See whether biased locking is enabled and if so set it for this
2027   // klass.
2028   // Note that this must be done past the last potential blocking
2029   // point / safepoint. We enable biased locking lazily using a
2030   // VM_Operation to iterate the SystemDictionary and installing the
2031   // biasable mark word into each InstanceKlass's prototype header.
2032   // To avoid race conditions where we accidentally miss enabling the
2033   // optimization for one class in the process of being added to the
2034   // dictionary, we must not safepoint after the test of
2035   // BiasedLocking::enabled().
2036   if (UseBiasedLocking && BiasedLocking::enabled()) {
2037     // Set biased locking bit for all loaded classes; it will be
2038     // cleared if revocation occurs too often for this type
2039     // NOTE that we must only do this when the class is initally
2040     // defined, not each time it is referenced from a new class loader
2041     if (k->class_loader() == class_loader()) {
2042       k->set_prototype_header(markOopDesc::biased_locking_prototype());
2043     }
2044   }
2045 






2046   // Make a new system dictionary entry.
2047   Klass* sd_check = find_class(d_index, d_hash, name, loader_data);
2048   if (sd_check == NULL) {
2049     dictionary()->add_klass(name, loader_data, k);
2050     notice_modification();
2051   }
2052 #ifdef ASSERT
2053   sd_check = find_class(d_index, d_hash, name, loader_data);
2054   assert (sd_check != NULL, "should have entry in system dictionary");
2055   // Note: there may be a placeholder entry: for circularity testing
2056   // or for parallel defines
2057 #endif
2058     SystemDictionary_lock->notify_all();
2059   }
2060 }
2061 
2062 
2063 // Try to find a class name using the loader constraints.  The
2064 // loader constraints might know about a class that isn't fully loaded
2065 // yet and these will be ignored.


2608   constraints()->verify(dictionary(), placeholders());
2609 }
2610 
2611 
2612 void SystemDictionary::verify_obj_klass_present(Symbol* class_name,
2613                                                 ClassLoaderData* loader_data) {
2614   GCMutexLocker mu(SystemDictionary_lock);
2615   Symbol* name;
2616 
2617   Klass* probe = find_class(class_name, loader_data);
2618   if (probe == NULL) {
2619     probe = SystemDictionary::find_shared_class(class_name);
2620     if (probe == NULL) {
2621       name = find_placeholder(class_name, loader_data);
2622     }
2623   }
2624   guarantee(probe != NULL || name != NULL,
2625             "Loaded klasses should be in SystemDictionary");
2626 }
2627 
2628 // utility function for class load event
2629 void SystemDictionary::post_class_load_event(TracingTime start_time,
2630                                              instanceKlassHandle k,
2631                                              Handle initiating_loader) {
2632 #if INCLUDE_TRACE
2633   EventClassLoad event(UNTIMED);
2634   if (event.should_commit()) {
2635     event.set_endtime(Tracing::time());
2636     event.set_starttime(start_time);
2637     event.set_loadedClass(k());
2638     oop defining_class_loader = k->class_loader();
2639     event.set_definingClassLoader(defining_class_loader !=  NULL ?
2640                                     defining_class_loader->klass() : (Klass*)NULL);
2641     oop class_loader = initiating_loader.is_null() ? (oop)NULL : initiating_loader();
2642     event.set_initiatingClassLoader(class_loader != NULL ?
2643                                       class_loader->klass() : (Klass*)NULL);
2644     event.commit();
2645   }
2646 #endif /* INCLUDE_TRACE */
2647 }
2648 
2649 #ifndef PRODUCT
2650 
2651 // statistics code
2652 class ClassStatistics: AllStatic {
2653  private:
2654   static int nclasses;        // number of classes
2655   static int nmethods;        // number of methods
2656   static int nmethoddata;     // number of methodData
2657   static int class_size;      // size of class objects in words
2658   static int method_size;     // size of method objects in words
2659   static int debug_size;      // size of debug info in methods
2660   static int methoddata_size; // size of methodData objects in words
2661 
2662   static void do_class(Klass* k) {
2663     nclasses++;
2664     class_size += k->size();
2665     if (k->oop_is_instance()) {
2666       InstanceKlass* ik = (InstanceKlass*)k;
2667       class_size += ik->methods()->size();
2668       class_size += ik->constants()->size();