src/hotspot/share/classfile/classLoaderData.hpp
changeset 51959 db0c3952de52
parent 51682 a30461a359f5
child 51973 2f1698b6db15
equal deleted inserted replaced
51958:53a4760e9fcc 51959:db0c3952de52
    51 // the types it defines.
    51 // the types it defines.
    52 //
    52 //
    53 // ClassLoaderData are stored in the runtime representation of classes,
    53 // ClassLoaderData are stored in the runtime representation of classes,
    54 // and provides iterators for root tracing and other GC operations.
    54 // and provides iterators for root tracing and other GC operations.
    55 
    55 
    56 class ClassLoaderData;
    56 class ClassLoaderDataGraph;
    57 class JNIMethodBlock;
    57 class JNIMethodBlock;
    58 class Metadebug;
       
    59 class ModuleEntry;
    58 class ModuleEntry;
    60 class PackageEntry;
    59 class PackageEntry;
    61 class ModuleEntryTable;
    60 class ModuleEntryTable;
    62 class PackageEntryTable;
    61 class PackageEntryTable;
    63 class DictionaryEntry;
    62 class DictionaryEntry;
    64 class Dictionary;
    63 class Dictionary;
    65 
       
    66 // GC root for walking class loader data created
       
    67 
       
    68 class ClassLoaderDataGraph : public AllStatic {
       
    69   friend class ClassLoaderData;
       
    70   friend class ClassLoaderDataGraphMetaspaceIterator;
       
    71   friend class ClassLoaderDataGraphKlassIteratorAtomic;
       
    72   friend class ClassLoaderDataGraphKlassIteratorStatic;
       
    73   friend class ClassLoaderDataGraphIterator;
       
    74   friend class VMStructs;
       
    75  private:
       
    76   // All CLDs (except the null CLD) can be reached by walking _head->_next->...
       
    77   static ClassLoaderData* _head;
       
    78   static ClassLoaderData* _unloading;
       
    79   // CMS support.
       
    80   static ClassLoaderData* _saved_head;
       
    81   static ClassLoaderData* _saved_unloading;
       
    82   static bool _should_purge;
       
    83 
       
    84   // Set if there's anything to purge in the deallocate lists or previous versions
       
    85   // during a safepoint after class unloading in a full GC.
       
    86   static bool _should_clean_deallocate_lists;
       
    87   static bool _safepoint_cleanup_needed;
       
    88 
       
    89   // OOM has been seen in metaspace allocation. Used to prevent some
       
    90   // allocations until class unloading
       
    91   static bool _metaspace_oom;
       
    92 
       
    93   static volatile size_t  _num_instance_classes;
       
    94   static volatile size_t  _num_array_classes;
       
    95 
       
    96   static ClassLoaderData* add_to_graph(Handle class_loader, bool is_unsafe_anonymous);
       
    97   static ClassLoaderData* add(Handle class_loader, bool is_unsafe_anonymous);
       
    98 
       
    99  public:
       
   100   static ClassLoaderData* find_or_create(Handle class_loader);
       
   101   static void clean_module_and_package_info();
       
   102   static void purge();
       
   103   static void clear_claimed_marks();
       
   104   // Iteration through CLDG inside a safepoint; GC support
       
   105   static void cld_do(CLDClosure* cl);
       
   106   static void cld_unloading_do(CLDClosure* cl);
       
   107   static void roots_cld_do(CLDClosure* strong, CLDClosure* weak);
       
   108   static void always_strong_cld_do(CLDClosure* cl);
       
   109   // klass do
       
   110   // Walking classes through the ClassLoaderDataGraph include array classes.  It also includes
       
   111   // classes that are allocated but not loaded, classes that have errors, and scratch classes
       
   112   // for redefinition.  These classes are removed during the next class unloading.
       
   113   // Walking the ClassLoaderDataGraph also includes unsafe anonymous classes.
       
   114   static void classes_do(KlassClosure* klass_closure);
       
   115   static void classes_do(void f(Klass* const));
       
   116   static void methods_do(void f(Method*));
       
   117   static void modules_do(void f(ModuleEntry*));
       
   118   static void modules_unloading_do(void f(ModuleEntry*));
       
   119   static void packages_do(void f(PackageEntry*));
       
   120   static void packages_unloading_do(void f(PackageEntry*));
       
   121   static void loaded_classes_do(KlassClosure* klass_closure);
       
   122   static void unlocked_loaded_classes_do(KlassClosure* klass_closure);
       
   123   static void classes_unloading_do(void f(Klass* const));
       
   124   static bool do_unloading(bool do_cleaning);
       
   125 
       
   126   // Expose state to avoid logging overhead in safepoint cleanup tasks.
       
   127   static inline bool should_clean_metaspaces_and_reset();
       
   128   static void set_should_clean_deallocate_lists() { _should_clean_deallocate_lists = true; }
       
   129   static void clean_deallocate_lists(bool purge_previous_versions);
       
   130   static void walk_metadata_and_clean_metaspaces();
       
   131 
       
   132   // dictionary do
       
   133   // Iterate over all klasses in dictionary, but
       
   134   // just the classes from defining class loaders.
       
   135   static void dictionary_classes_do(void f(InstanceKlass*));
       
   136   // Added for initialize_itable_for_klass to handle exceptions.
       
   137   static void dictionary_classes_do(void f(InstanceKlass*, TRAPS), TRAPS);
       
   138 
       
   139   // VM_CounterDecay iteration support
       
   140   static InstanceKlass* try_get_next_class();
       
   141 
       
   142   static void verify_dictionary();
       
   143   static void print_dictionary(outputStream* st);
       
   144   static void print_dictionary_statistics(outputStream* st);
       
   145 
       
   146   // CMS support.
       
   147   static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
       
   148   static GrowableArray<ClassLoaderData*>* new_clds();
       
   149 
       
   150   static void set_should_purge(bool b) { _should_purge = b; }
       
   151   static void purge_if_needed() {
       
   152     // Only purge the CLDG for CMS if concurrent sweep is complete.
       
   153     if (_should_purge) {
       
   154       purge();
       
   155       // reset for next time.
       
   156       set_should_purge(false);
       
   157     }
       
   158   }
       
   159 
       
   160   static int resize_if_needed();
       
   161 
       
   162   static bool has_metaspace_oom()           { return _metaspace_oom; }
       
   163   static void set_metaspace_oom(bool value) { _metaspace_oom = value; }
       
   164 
       
   165   static void print_on(outputStream * const out) PRODUCT_RETURN;
       
   166   static void print() { print_on(tty); }
       
   167   static void verify();
       
   168 
       
   169   // instance and array class counters
       
   170   static inline size_t num_instance_classes();
       
   171   static inline size_t num_array_classes();
       
   172   static inline void inc_instance_classes(size_t count);
       
   173   static inline void dec_instance_classes(size_t count);
       
   174   static inline void inc_array_classes(size_t count);
       
   175   static inline void dec_array_classes(size_t count);
       
   176 
       
   177 #ifndef PRODUCT
       
   178   static bool contains_loader_data(ClassLoaderData* loader_data);
       
   179 #endif
       
   180 };
       
   181 
       
   182 class LockedClassesDo : public KlassClosure {
       
   183   typedef void (*classes_do_func_t)(Klass*);
       
   184   classes_do_func_t _function;
       
   185 public:
       
   186   LockedClassesDo();  // For callers who provide their own do_klass
       
   187   LockedClassesDo(classes_do_func_t function);
       
   188   ~LockedClassesDo();
       
   189 
       
   190   void do_klass(Klass* k) {
       
   191     (*_function)(k);
       
   192   }
       
   193 };
       
   194 
       
   195 
    64 
   196 // ClassLoaderData class
    65 // ClassLoaderData class
   197 
    66 
   198 class ClassLoaderData : public CHeapObj<mtClass> {
    67 class ClassLoaderData : public CHeapObj<mtClass> {
   199   friend class VMStructs;
    68   friend class VMStructs;
   446   Symbol* name_and_id() const { return _name_and_id; }
   315   Symbol* name_and_id() const { return _name_and_id; }
   447 
   316 
   448   JFR_ONLY(DEFINE_TRACE_ID_METHODS;)
   317   JFR_ONLY(DEFINE_TRACE_ID_METHODS;)
   449 };
   318 };
   450 
   319 
   451 // An iterator that distributes Klasses to parallel worker threads.
       
   452 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj {
       
   453  Klass* volatile _next_klass;
       
   454  public:
       
   455   ClassLoaderDataGraphKlassIteratorAtomic();
       
   456   Klass* next_klass();
       
   457  private:
       
   458   static Klass* next_klass_in_cldg(Klass* klass);
       
   459 };
       
   460 
       
   461 class ClassLoaderDataGraphMetaspaceIterator : public StackObj {
       
   462   ClassLoaderData* _data;
       
   463  public:
       
   464   ClassLoaderDataGraphMetaspaceIterator();
       
   465   ~ClassLoaderDataGraphMetaspaceIterator();
       
   466   bool repeat() { return _data != NULL; }
       
   467   ClassLoaderMetaspace* get_next() {
       
   468     assert(_data != NULL, "Should not be NULL in call to the iterator");
       
   469     ClassLoaderMetaspace* result = _data->metaspace_or_null();
       
   470     _data = _data->next();
       
   471     // This result might be NULL for class loaders without metaspace
       
   472     // yet.  It would be nice to return only non-null results but
       
   473     // there is no guarantee that there will be a non-null result
       
   474     // down the list so the caller is going to have to check.
       
   475     return result;
       
   476   }
       
   477 };
       
   478 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
   320 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP