< prev index next >

src/share/vm/classfile/classLoaderData.hpp

Print this page




  40 //
  41 // A class loader represents a linkset. Conceptually, a linkset identifies
  42 // the complete transitive closure of resolved links that a dynamic linker can
  43 // produce.
  44 //
  45 // A ClassLoaderData also encapsulates the allocation space, called a metaspace,
  46 // used by the dynamic linker to allocate the runtime representation of all
  47 // the types it defines.
  48 //
  49 // ClassLoaderData are stored in the runtime representation of classes and the
  50 // system dictionary, are roots of garbage collection, and provides iterators
  51 // for root tracing and other GC operations.
  52 
  53 class ClassLoaderData;
  54 class JNIMethodBlock;
  55 class Metadebug;
  56 class ModuleEntry;
  57 class PackageEntry;
  58 class ModuleEntryTable;
  59 class PackageEntryTable;


  60 
  61 // GC root for walking class loader data created
  62 
  63 class ClassLoaderDataGraph : public AllStatic {
  64   friend class ClassLoaderData;
  65   friend class ClassLoaderDataGraphMetaspaceIterator;
  66   friend class ClassLoaderDataGraphKlassIteratorAtomic;

  67   friend class VMStructs;
  68  private:
  69   // All CLDs (except the null CLD) can be reached by walking _head->_next->...
  70   static ClassLoaderData* _head;
  71   static ClassLoaderData* _unloading;
  72   // CMS support.
  73   static ClassLoaderData* _saved_head;
  74   static ClassLoaderData* _saved_unloading;
  75   static bool _should_purge;
  76   // OOM has been seen in metaspace allocation. Used to prevent some
  77   // allocations until class unloading
  78   static bool _metaspace_oom;
  79 
  80   static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS);
  81   static void post_class_unload_events();
  82  public:
  83   static ClassLoaderData* find_or_create(Handle class_loader, TRAPS);
  84   static void purge();
  85   static void clear_claimed_marks();
  86   // oops do


  92   static void cld_unloading_do(CLDClosure* cl);
  93   static void roots_cld_do(CLDClosure* strong, CLDClosure* weak);
  94   static void keep_alive_cld_do(CLDClosure* cl);
  95   static void always_strong_cld_do(CLDClosure* cl);
  96   // klass do
  97   // Walking classes through the ClassLoaderDataGraph include array classes.  It also includes
  98   // classes that are allocated but not loaded, classes that have errors, and scratch classes
  99   // for redefinition.  These classes are removed during the next class unloading.
 100   // Walking the ClassLoaderDataGraph also includes anonymous classes.
 101   static void classes_do(KlassClosure* klass_closure);
 102   static void classes_do(void f(Klass* const));
 103   static void methods_do(void f(Method*));
 104   static void modules_do(void f(ModuleEntry*));
 105   static void modules_unloading_do(void f(ModuleEntry*));
 106   static void packages_do(void f(PackageEntry*));
 107   static void packages_unloading_do(void f(PackageEntry*));
 108   static void loaded_classes_do(KlassClosure* klass_closure);
 109   static void classes_unloading_do(void f(Klass* const));
 110   static bool do_unloading(BoolObjectClosure* is_alive, bool clean_previous_versions);
 111 
















 112   // CMS support.
 113   static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
 114   static GrowableArray<ClassLoaderData*>* new_clds();
 115 
 116   static void set_should_purge(bool b) { _should_purge = b; }
 117   static void purge_if_needed() {
 118     // Only purge the CLDG for CMS if concurrent sweep is complete.
 119     if (_should_purge) {
 120       purge();
 121       // reset for next time.
 122       set_should_purge(false);
 123     }
 124   }
 125 
 126   static bool has_metaspace_oom()           { return _metaspace_oom; }
 127   static void set_metaspace_oom(bool value) { _metaspace_oom = value; }
 128 
 129   static void dump_on(outputStream * const out) PRODUCT_RETURN;
 130   static void dump() { dump_on(tty); }
 131   static void verify();


 176 
 177     Chunk* _head;
 178 
 179     void oops_do_chunk(OopClosure* f, Chunk* c, const juint size);
 180 
 181    public:
 182     ChunkedHandleList() : _head(NULL) {}
 183     ~ChunkedHandleList();
 184 
 185     // Only one thread at a time can add, guarded by ClassLoaderData::metaspace_lock().
 186     // However, multiple threads can execute oops_do concurrently with add.
 187     oop* add(oop o);
 188 #ifdef ASSERT
 189     bool contains(oop* p);
 190 #endif
 191     void oops_do(OopClosure* f);
 192   };
 193 
 194   friend class ClassLoaderDataGraph;
 195   friend class ClassLoaderDataGraphKlassIteratorAtomic;

 196   friend class ClassLoaderDataGraphMetaspaceIterator;
 197   friend class MetaDataFactory;
 198   friend class Method;
 199 
 200   static ClassLoaderData * _the_null_class_loader_data;
 201 
 202   oop _class_loader;          // oop used to uniquely identify a class loader
 203                               // class loader or a canonical class path
 204   Dependencies _dependencies; // holds dependencies from this class loader
 205                               // data to others.
 206 
 207   Metaspace * volatile _metaspace;  // Meta-space where meta-data defined by the
 208                                     // classes in the class loader are allocated.
 209   Mutex* _metaspace_lock;  // Locks the metaspace for allocations and setup.
 210   bool _unloading;         // true if this class loader goes away
 211   bool _is_anonymous;      // if this CLD is for an anonymous class
 212   s2 _keep_alive;          // if this CLD is kept alive without a keep_alive_object().
 213                            // Used for anonymous classes and the boot class
 214                            // loader. _keep_alive does not need to be volatile or
 215                            // atomic since there is one unique CLD per anonymous class.
 216   volatile int _claimed;   // true if claimed, for example during GC traces.
 217                            // To avoid applying oop closure more than once.
 218                            // Has to be an int because we cas it.
 219   ChunkedHandleList _handles; // Handles to constant pool arrays, Modules, etc, which
 220                               // have the same life cycle of the corresponding ClassLoader.
 221 
 222   Klass* volatile _klasses;              // The classes defined by the class loader.
 223   PackageEntryTable* volatile _packages; // The packages defined by the class loader.
 224   ModuleEntry* _unnamed_module;          // This class loader's unnamed module.
 225   ModuleEntryTable* volatile _modules;   // The modules defined by the class loader.

 226 
 227   // These method IDs are created for the class loader and set to NULL when the
 228   // class loader is unloaded.  They are rarely freed, only for redefine classes
 229   // and if they lose a data race in InstanceKlass.
 230   JNIMethodBlock*                  _jmethod_ids;
 231 
 232   // Metadata to be deallocated when it's safe at class unloading, when
 233   // this class loader isn't unloaded itself.
 234   GrowableArray<Metadata*>*      _deallocate_list;
 235 
 236   // Support for walking class loader data objects
 237   ClassLoaderData* _next; /// Next loader_datas created
 238 
 239   // ReadOnly and ReadWrite metaspaces (static because only on the null
 240   // class loader for now).
 241   static Metaspace* _ro_metaspace;
 242   static Metaspace* _rw_metaspace;
 243 
 244   TRACE_DEFINE_TRACE_ID_FIELD;
 245 


 307   // The Metaspace is created lazily so may be NULL.  This
 308   // method will allocate a Metaspace if needed.
 309   Metaspace* metaspace_non_null();
 310 
 311   oop class_loader() const      { return _class_loader; }
 312 
 313   // The object the GC is using to keep this ClassLoaderData alive.
 314   oop keep_alive_object() const;
 315 
 316   // Returns true if this class loader data is for a loader going away.
 317   bool is_unloading() const     {
 318     assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");
 319     return _unloading;
 320   }
 321 
 322   // Used to refcount an anonymous class's CLD in order to
 323   // indicate their aliveness without a keep_alive_object().
 324   void inc_keep_alive();
 325   void dec_keep_alive();
 326 
 327   inline unsigned int identity_hash() const;
 328 
 329   // Used when tracing from klasses.
 330   void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
 331 
 332   void classes_do(KlassClosure* klass_closure);

 333 
 334   JNIMethodBlock* jmethod_ids() const              { return _jmethod_ids; }
 335   void set_jmethod_ids(JNIMethodBlock* new_block)  { _jmethod_ids = new_block; }
 336 


 337   void print_value() { print_value_on(tty); }
 338   void print_value_on(outputStream* out) const;
 339   void dump(outputStream * const out) PRODUCT_RETURN;
 340   void verify();
 341   const char* loader_name();
 342 
 343   jobject add_handle(Handle h);
 344   void remove_handle_unsafe(jobject h);
 345   void add_class(Klass* k, bool publicize = true);
 346   void remove_class(Klass* k);
 347   bool contains_klass(Klass* k);
 348   void record_dependency(const Klass* to, TRAPS);
 349   void init_dependencies(TRAPS);
 350   PackageEntryTable* packages() { return _packages; }
 351   ModuleEntry* unnamed_module() { return _unnamed_module; }
 352   ModuleEntryTable* modules();
 353   bool modules_defined() { return (_modules != NULL); }




 354 
 355   void add_to_deallocate_list(Metadata* m);
 356 
 357   static ClassLoaderData* class_loader_data(oop loader);
 358   static ClassLoaderData* class_loader_data_or_null(oop loader);
 359   static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);
 360   static void print_loader(ClassLoaderData *loader_data, outputStream *out);
 361 
 362   // CDS support
 363   Metaspace* ro_metaspace();
 364   Metaspace* rw_metaspace();
 365   void initialize_shared_metaspaces();
 366 
 367   TRACE_DEFINE_TRACE_ID_METHODS;
 368 };
 369 
 370 // An iterator that distributes Klasses to parallel worker threads.
 371 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj {
 372  Klass* volatile _next_klass;
 373  public:




  40 //
  41 // A class loader represents a linkset. Conceptually, a linkset identifies
  42 // the complete transitive closure of resolved links that a dynamic linker can
  43 // produce.
  44 //
  45 // A ClassLoaderData also encapsulates the allocation space, called a metaspace,
  46 // used by the dynamic linker to allocate the runtime representation of all
  47 // the types it defines.
  48 //
  49 // ClassLoaderData are stored in the runtime representation of classes and the
  50 // system dictionary, are roots of garbage collection, and provides iterators
  51 // for root tracing and other GC operations.
  52 
  53 class ClassLoaderData;
  54 class JNIMethodBlock;
  55 class Metadebug;
  56 class ModuleEntry;
  57 class PackageEntry;
  58 class ModuleEntryTable;
  59 class PackageEntryTable;
  60 class DictionaryEntry;
  61 class Dictionary;
  62 
  63 // GC root for walking class loader data created
  64 
  65 class ClassLoaderDataGraph : public AllStatic {
  66   friend class ClassLoaderData;
  67   friend class ClassLoaderDataGraphMetaspaceIterator;
  68   friend class ClassLoaderDataGraphKlassIteratorAtomic;
  69   friend class ClassLoaderDataGraphKlassIteratorStatic;
  70   friend class VMStructs;
  71  private:
  72   // All CLDs (except the null CLD) can be reached by walking _head->_next->...
  73   static ClassLoaderData* _head;
  74   static ClassLoaderData* _unloading;
  75   // CMS support.
  76   static ClassLoaderData* _saved_head;
  77   static ClassLoaderData* _saved_unloading;
  78   static bool _should_purge;
  79   // OOM has been seen in metaspace allocation. Used to prevent some
  80   // allocations until class unloading
  81   static bool _metaspace_oom;
  82 
  83   static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS);
  84   static void post_class_unload_events();
  85  public:
  86   static ClassLoaderData* find_or_create(Handle class_loader, TRAPS);
  87   static void purge();
  88   static void clear_claimed_marks();
  89   // oops do


  95   static void cld_unloading_do(CLDClosure* cl);
  96   static void roots_cld_do(CLDClosure* strong, CLDClosure* weak);
  97   static void keep_alive_cld_do(CLDClosure* cl);
  98   static void always_strong_cld_do(CLDClosure* cl);
  99   // klass do
 100   // Walking classes through the ClassLoaderDataGraph include array classes.  It also includes
 101   // classes that are allocated but not loaded, classes that have errors, and scratch classes
 102   // for redefinition.  These classes are removed during the next class unloading.
 103   // Walking the ClassLoaderDataGraph also includes anonymous classes.
 104   static void classes_do(KlassClosure* klass_closure);
 105   static void classes_do(void f(Klass* const));
 106   static void methods_do(void f(Method*));
 107   static void modules_do(void f(ModuleEntry*));
 108   static void modules_unloading_do(void f(ModuleEntry*));
 109   static void packages_do(void f(PackageEntry*));
 110   static void packages_unloading_do(void f(PackageEntry*));
 111   static void loaded_classes_do(KlassClosure* klass_closure);
 112   static void classes_unloading_do(void f(Klass* const));
 113   static bool do_unloading(BoolObjectClosure* is_alive, bool clean_previous_versions);
 114 
 115   // dictionary do
 116   // Iterate over all klasses in dictionary, but
 117   // just the classes from defining class loaders.
 118   static void dictionary_classes_do(void f(InstanceKlass*));
 119   // Added for initialize_itable_for_klass to handle exceptions.
 120   static void dictionary_classes_do(void f(InstanceKlass*, TRAPS), TRAPS);
 121 
 122   // Iterate all classes and their class loaders, including initiating class loaders.
 123   static void dictionary_all_entries_do(void f(InstanceKlass*, ClassLoaderData*));
 124 
 125   // VM_CounterDecay iteration support
 126   static InstanceKlass* try_get_next_class();
 127 
 128   static void verify_dictionary();
 129   static void print_dictionary(bool details);
 130 
 131   // CMS support.
 132   static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
 133   static GrowableArray<ClassLoaderData*>* new_clds();
 134 
 135   static void set_should_purge(bool b) { _should_purge = b; }
 136   static void purge_if_needed() {
 137     // Only purge the CLDG for CMS if concurrent sweep is complete.
 138     if (_should_purge) {
 139       purge();
 140       // reset for next time.
 141       set_should_purge(false);
 142     }
 143   }
 144 
 145   static bool has_metaspace_oom()           { return _metaspace_oom; }
 146   static void set_metaspace_oom(bool value) { _metaspace_oom = value; }
 147 
 148   static void dump_on(outputStream * const out) PRODUCT_RETURN;
 149   static void dump() { dump_on(tty); }
 150   static void verify();


 195 
 196     Chunk* _head;
 197 
 198     void oops_do_chunk(OopClosure* f, Chunk* c, const juint size);
 199 
 200    public:
 201     ChunkedHandleList() : _head(NULL) {}
 202     ~ChunkedHandleList();
 203 
 204     // Only one thread at a time can add, guarded by ClassLoaderData::metaspace_lock().
 205     // However, multiple threads can execute oops_do concurrently with add.
 206     oop* add(oop o);
 207 #ifdef ASSERT
 208     bool contains(oop* p);
 209 #endif
 210     void oops_do(OopClosure* f);
 211   };
 212 
 213   friend class ClassLoaderDataGraph;
 214   friend class ClassLoaderDataGraphKlassIteratorAtomic;
 215   friend class ClassLoaderDataGraphKlassIteratorStatic;
 216   friend class ClassLoaderDataGraphMetaspaceIterator;
 217   friend class MetaDataFactory;
 218   friend class Method;
 219 
 220   static ClassLoaderData * _the_null_class_loader_data;
 221 
 222   oop _class_loader;          // oop used to uniquely identify a class loader
 223                               // class loader or a canonical class path
 224   Dependencies _dependencies; // holds dependencies from this class loader
 225                               // data to others.
 226 
 227   Metaspace * volatile _metaspace;  // Meta-space where meta-data defined by the
 228                                     // classes in the class loader are allocated.
 229   Mutex* _metaspace_lock;  // Locks the metaspace for allocations and setup.
 230   bool _unloading;         // true if this class loader goes away
 231   bool _is_anonymous;      // if this CLD is for an anonymous class
 232   s2 _keep_alive;          // if this CLD is kept alive without a keep_alive_object().
 233                            // Used for anonymous classes and the boot class
 234                            // loader. _keep_alive does not need to be volatile or
 235                            // atomic since there is one unique CLD per anonymous class.
 236   volatile int _claimed;   // true if claimed, for example during GC traces.
 237                            // To avoid applying oop closure more than once.
 238                            // Has to be an int because we cas it.
 239   ChunkedHandleList _handles; // Handles to constant pool arrays, Modules, etc, which
 240                               // have the same life cycle of the corresponding ClassLoader.
 241 
 242   Klass* volatile _klasses;              // The classes defined by the class loader.
 243   PackageEntryTable* volatile _packages; // The packages defined by the class loader.
 244   ModuleEntry* _unnamed_module;          // This class loader's unnamed module.
 245   ModuleEntryTable* volatile _modules;   // The modules defined by the class loader.
 246   Dictionary*       volatile _dictionary; // The loaded InstanceKlasses, including initiated by this class loader
 247 
 248   // These method IDs are created for the class loader and set to NULL when the
 249   // class loader is unloaded.  They are rarely freed, only for redefine classes
 250   // and if they lose a data race in InstanceKlass.
 251   JNIMethodBlock*                  _jmethod_ids;
 252 
 253   // Metadata to be deallocated when it's safe at class unloading, when
 254   // this class loader isn't unloaded itself.
 255   GrowableArray<Metadata*>*      _deallocate_list;
 256 
 257   // Support for walking class loader data objects
 258   ClassLoaderData* _next; /// Next loader_datas created
 259 
 260   // ReadOnly and ReadWrite metaspaces (static because only on the null
 261   // class loader for now).
 262   static Metaspace* _ro_metaspace;
 263   static Metaspace* _rw_metaspace;
 264 
 265   TRACE_DEFINE_TRACE_ID_FIELD;
 266 


 328   // The Metaspace is created lazily so may be NULL.  This
 329   // method will allocate a Metaspace if needed.
 330   Metaspace* metaspace_non_null();
 331 
 332   oop class_loader() const      { return _class_loader; }
 333 
 334   // The object the GC is using to keep this ClassLoaderData alive.
 335   oop keep_alive_object() const;
 336 
 337   // Returns true if this class loader data is for a loader going away.
 338   bool is_unloading() const     {
 339     assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");
 340     return _unloading;
 341   }
 342 
 343   // Used to refcount an anonymous class's CLD in order to
 344   // indicate their aliveness without a keep_alive_object().
 345   void inc_keep_alive();
 346   void dec_keep_alive();
 347 
 348   inline unsigned int identity_hash() const { return (unsigned int)(((intptr_t)this) >> 3); }
 349 
 350   // Used when tracing from klasses.
 351   void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
 352 
 353   void classes_do(KlassClosure* klass_closure);
 354   Klass* klasses() { return _klasses; }
 355 
 356   JNIMethodBlock* jmethod_ids() const              { return _jmethod_ids; }
 357   void set_jmethod_ids(JNIMethodBlock* new_block)  { _jmethod_ids = new_block; }
 358 
 359   void print()                                     { print_on(tty); }
 360   void print_on(outputStream* out) const;
 361   void print_value()                               { print_value_on(tty); }
 362   void print_value_on(outputStream* out) const;
 363   void dump(outputStream * const out) PRODUCT_RETURN;
 364   void verify();
 365   const char* loader_name();
 366 
 367   jobject add_handle(Handle h);
 368   void remove_handle_unsafe(jobject h);
 369   void add_class(Klass* k, bool publicize = true);
 370   void remove_class(Klass* k);
 371   bool contains_klass(Klass* k);
 372   void record_dependency(const Klass* to, TRAPS);
 373   void init_dependencies(TRAPS);
 374   PackageEntryTable* packages() { return _packages; }
 375   ModuleEntry* unnamed_module() { return _unnamed_module; }
 376   ModuleEntryTable* modules();
 377   bool modules_defined() { return (_modules != NULL); }
 378 
 379   // Loaded class dictionary
 380   Dictionary* dictionary();
 381   Dictionary* dictionary_or_null();
 382 
 383   void add_to_deallocate_list(Metadata* m);
 384 
 385   static ClassLoaderData* class_loader_data(oop loader);
 386   static ClassLoaderData* class_loader_data_or_null(oop loader);
 387   static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);
 388   static void print_loader(ClassLoaderData *loader_data, outputStream *out);
 389 
 390   // CDS support
 391   Metaspace* ro_metaspace();
 392   Metaspace* rw_metaspace();
 393   void initialize_shared_metaspaces();
 394 
 395   TRACE_DEFINE_TRACE_ID_METHODS;
 396 };
 397 
 398 // An iterator that distributes Klasses to parallel worker threads.
 399 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj {
 400  Klass* volatile _next_klass;
 401  public:


< prev index next >