< prev index next >

src/hotspot/share/classfile/classLoaderData.hpp

Print this page




   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
  26 #define SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "memory/memRegion.hpp"
  30 #include "memory/metaspace.hpp"
  31 #include "memory/metaspaceCounters.hpp"
  32 #include "oops/oopHandle.hpp"
  33 #include "runtime/mutex.hpp"
  34 #include "trace/traceMacros.hpp"
  35 #include "utilities/growableArray.hpp"
  36 #include "utilities/macros.hpp"
  37 #if INCLUDE_TRACE
  38 #include "utilities/ticks.hpp"
  39 #endif
  40 
  41 //
  42 // A class loader represents a linkset. Conceptually, a linkset identifies
  43 // the complete transitive closure of resolved links that a dynamic linker can
  44 // produce.
  45 //
  46 // A ClassLoaderData also encapsulates the allocation space, called a metaspace,
  47 // used by the dynamic linker to allocate the runtime representation of all
  48 // the types it defines.
  49 //
  50 // ClassLoaderData are stored in the runtime representation of classes,
  51 // and provides iterators 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
  90   static void oops_do(OopClosure* f, bool must_claim);
  91   static void keep_alive_oops_do(OopClosure* blk, bool must_claim);
  92   static void always_strong_oops_do(OopClosure* blk, bool must_claim);
  93   // cld do
  94   static void cld_do(CLDClosure* cl);
  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);


 132   // CMS support.
 133   static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
 134   static GrowableArray<ClassLoaderData*>* new_clds();
 135 
 136   static void set_should_purge(bool b) { _should_purge = b; }
 137   static void purge_if_needed() {
 138     // Only purge the CLDG for CMS if concurrent sweep is complete.
 139     if (_should_purge) {
 140       purge();
 141       // reset for next time.
 142       set_should_purge(false);
 143     }
 144   }
 145 
 146   static int resize_if_needed();
 147 
 148   static bool has_metaspace_oom()           { return _metaspace_oom; }
 149   static void set_metaspace_oom(bool value) { _metaspace_oom = value; }
 150 
 151   static void dump_on(outputStream * const out) PRODUCT_RETURN;
 152   static void dump() { dump_on(tty); }
 153   static void verify();
 154   static void print_creation(outputStream* out, Handle loader, ClassLoaderData* cld, TRAPS);
 155 
 156   static bool unload_list_contains(const void* x);
 157 #ifndef PRODUCT
 158   static bool contains_loader_data(ClassLoaderData* loader_data);
 159 #endif
 160 
 161 #if INCLUDE_TRACE
 162  private:
 163   static Ticks _class_unload_time;
 164   static void class_unload_event(Klass* const k);
 165 #endif
 166 };
 167 
 168 // ClassLoaderData class
 169 
 170 class ClassLoaderData : public CHeapObj<mtClass> {







 171   friend class VMStructs;
 172  private:
 173   class Dependencies VALUE_OBJ_CLASS_SPEC {



 174     objArrayOop _list_head;
 175     void locked_add(objArrayHandle last,
 176                     objArrayHandle new_dependency,
 177                     Thread* THREAD);
 178    public:
 179     Dependencies() : _list_head(NULL) {}
 180     Dependencies(TRAPS) : _list_head(NULL) {
 181       init(CHECK);
 182     }
 183     void add(Handle dependency, TRAPS);
 184     void init(TRAPS);


 185     void oops_do(OopClosure* f);
 186   };
 187 
 188   class ChunkedHandleList VALUE_OBJ_CLASS_SPEC {


 189     struct Chunk : public CHeapObj<mtClass> {
 190       static const size_t CAPACITY = 32;
 191 
 192       oop _data[CAPACITY];
 193       volatile juint _size;
 194       Chunk* _next;
 195 
 196       Chunk(Chunk* c) : _next(c), _size(0) { }
 197     };
 198 
 199     Chunk* volatile _head;
 200 
 201     void oops_do_chunk(OopClosure* f, Chunk* c, const juint size);
 202 
 203    public:
 204     ChunkedHandleList() : _head(NULL) {}
 205     ~ChunkedHandleList();
 206 
 207     // Only one thread at a time can add, guarded by ClassLoaderData::metaspace_lock().
 208     // However, multiple threads can execute oops_do concurrently with add.
 209     oop* add(oop o);
 210 #ifdef ASSERT
 211     bool contains(oop* p);
 212 #endif
 213     void oops_do(OopClosure* f);


 214   };
 215 
 216   friend class ClassLoaderDataGraph;
 217   friend class ClassLoaderDataGraphKlassIteratorAtomic;
 218   friend class ClassLoaderDataGraphKlassIteratorStatic;
 219   friend class ClassLoaderDataGraphMetaspaceIterator;
 220   friend class MetaDataFactory;
 221   friend class Method;
 222 
 223   static ClassLoaderData * _the_null_class_loader_data;




 224 
 225   oop _class_loader;          // oop used to uniquely identify a class loader
 226                               // class loader or a canonical class path
 227   Dependencies _dependencies; // holds dependencies from this class loader
 228                               // data to others.
 229 
 230   Metaspace * volatile _metaspace;  // Meta-space where meta-data defined by the
 231                                     // classes in the class loader are allocated.
 232   Mutex* _metaspace_lock;  // Locks the metaspace for allocations and setup.
 233   bool _unloading;         // true if this class loader goes away
 234   bool _is_anonymous;      // if this CLD is for an anonymous class
 235 
 236   // Remembered sets support for the oops in the class loader data.
 237   bool _modified_oops;             // Card Table Equivalent (YC/CMS support)
 238   bool _accumulated_modified_oops; // Mod Union Equivalent (CMS support)
 239 
 240   s2 _keep_alive;          // if this CLD is kept alive without a keep_alive_object().
 241                            // Used for anonymous classes and the boot class
 242                            // loader. _keep_alive does not need to be volatile or
 243                            // atomic since there is one unique CLD per anonymous class.
 244 
 245   volatile int _claimed;   // true if claimed, for example during GC traces.
 246                            // To avoid applying oop closure more than once.
 247                            // Has to be an int because we cas it.
 248   ChunkedHandleList _handles; // Handles to constant pool arrays, Modules, etc, which
 249                               // have the same life cycle of the corresponding ClassLoader.
 250 
 251   Klass* volatile _klasses;              // The classes defined by the class loader.
 252   PackageEntryTable* volatile _packages; // The packages defined by the class loader.
 253   ModuleEntryTable*  volatile _modules;  // The modules defined by the class loader.
 254   ModuleEntry* _unnamed_module;          // This class loader's unnamed module.
 255   Dictionary*  _dictionary;              // The loaded InstanceKlasses, including initiated by this class loader
 256 
 257   // These method IDs are created for the class loader and set to NULL when the
 258   // class loader is unloaded.  They are rarely freed, only for redefine classes
 259   // and if they lose a data race in InstanceKlass.
 260   JNIMethodBlock*                  _jmethod_ids;
 261 
 262   // Metadata to be deallocated when it's safe at class unloading, when
 263   // this class loader isn't unloaded itself.
 264   GrowableArray<Metadata*>*      _deallocate_list;
 265 
 266   // Support for walking class loader data objects
 267   ClassLoaderData* _next; /// Next loader_datas created
 268 
 269   // ReadOnly and ReadWrite metaspaces (static because only on the null
 270   // class loader for now).
 271   static Metaspace* _ro_metaspace;
 272   static Metaspace* _rw_metaspace;




 273 
 274   TRACE_DEFINE_TRACE_ID_FIELD;
 275 







 276   void set_next(ClassLoaderData* next) { _next = next; }
 277   ClassLoaderData* next() const        { return _next; }
 278 
 279   ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies);
 280   ~ClassLoaderData();
 281 
 282   // GC interface.
 283   void clear_claimed()          { _claimed = 0; }
 284   bool claimed() const          { return _claimed == 1; }
 285   bool claim();




 286 
 287   // The CLD are not placed in the Heap, so the Card Table or
 288   // the Mod Union Table can't be used to mark when CLD have modified oops.
 289   // The CT and MUT bits saves this information for the whole class loader data.
 290   void clear_modified_oops()             { _modified_oops = false; }
 291  public:
 292   void record_modified_oops()            { _modified_oops = true; }
 293   bool has_modified_oops()               { return _modified_oops; }
 294 
 295   void accumulate_modified_oops()        { if (has_modified_oops()) _accumulated_modified_oops = true; }
 296   void clear_accumulated_modified_oops() { _accumulated_modified_oops = false; }
 297   bool has_accumulated_modified_oops()   { return _accumulated_modified_oops; }
 298  private:
 299 
 300   void unload();
 301   bool keep_alive() const       { return _keep_alive > 0; }
 302   void classes_do(void f(Klass*));
 303   void loaded_classes_do(KlassClosure* klass_closure);
 304   void classes_do(void f(InstanceKlass*));

 305   void methods_do(void f(Method*));
 306   void modules_do(void f(ModuleEntry*));
 307   void packages_do(void f(PackageEntry*));
 308 
 309   // Deallocate free list during class unloading.
 310   void free_deallocate_list();      // for the classes that are not unloaded
 311   void unload_deallocate_list();    // for the classes that are unloaded
 312 
 313   // Allocate out of this class loader data
 314   MetaWord* allocate(size_t size);
 315 
 316   Dictionary* create_dictionary();
 317  public:


 318 
 319   bool is_alive(BoolObjectClosure* is_alive_closure) const;
 320 
 321   // Accessors
 322   Metaspace* metaspace_or_null() const     { return _metaspace; }
 323 
 324   static ClassLoaderData* the_null_class_loader_data() {
 325     return _the_null_class_loader_data;
 326   }
 327 
 328   Mutex* metaspace_lock() const { return _metaspace_lock; }
 329 
 330   bool is_anonymous() const { return _is_anonymous; }
 331 
 332   static void init_null_class_loader_data() {
 333     assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
 334     assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
 335 
 336     // We explicitly initialize the Dependencies object at a later phase in the initialization
 337     _the_null_class_loader_data = new ClassLoaderData(Handle(), false, Dependencies());
 338     ClassLoaderDataGraph::_head = _the_null_class_loader_data;
 339     assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
 340   }
 341 
 342   bool is_the_null_class_loader_data() const {
 343     return this == _the_null_class_loader_data;
 344   }
 345   bool is_system_class_loader_data() const;
 346   bool is_platform_class_loader_data() const;
 347   bool is_builtin_class_loader_data() const;
 348 
 349   // The Metaspace is created lazily so may be NULL.  This
 350   // method will allocate a Metaspace if needed.
 351   Metaspace* metaspace_non_null();
 352 
 353   oop class_loader() const      { return _class_loader; }


 354 

 355   // The object the GC is using to keep this ClassLoaderData alive.
 356   oop keep_alive_object() const;




 357 
 358   // Returns true if this class loader data is for a loader going away.
 359   bool is_unloading() const     {
 360     assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");
 361     return _unloading;
 362   }
 363 
 364   // Used to refcount an anonymous class's CLD in order to
 365   // indicate their aliveness without a keep_alive_object().
 366   void inc_keep_alive();
 367   void dec_keep_alive();
 368 
 369   inline unsigned int identity_hash() const { return (unsigned int)(((intptr_t)this) >> 3); }




 370 
 371   void oops_do(OopClosure* f, bool must_claim, bool clear_modified_oops = false);










 372 
 373   void classes_do(KlassClosure* klass_closure);
 374   Klass* klasses() { return _klasses; }




 375 
 376   JNIMethodBlock* jmethod_ids() const              { return _jmethod_ids; }
 377   void set_jmethod_ids(JNIMethodBlock* new_block)  { _jmethod_ids = new_block; }
 378 
 379   void print()                                     { print_on(tty); }
 380   void print_on(outputStream* out) const;
 381   void print_value()                               { print_value_on(tty); }
 382   void print_value_on(outputStream* out) const;
 383   void dump(outputStream * const out) PRODUCT_RETURN;
 384   void verify();
 385   const char* loader_name();
 386 
 387   OopHandle add_handle(Handle h);
 388   void remove_handle(OopHandle h);
 389   void init_handle_locked(OopHandle& pd, Handle h);  // used for concurrent access to ModuleEntry::_pd field
 390   void add_class(Klass* k, bool publicize = true);
 391   void remove_class(Klass* k);
 392   bool contains_klass(Klass* k);
 393   void record_dependency(const Klass* to, TRAPS);
 394   void init_dependencies(TRAPS);
 395   PackageEntryTable* packages() { return _packages; }
 396   ModuleEntry* unnamed_module() { return _unnamed_module; }
 397   ModuleEntryTable* modules();
 398   bool modules_defined() { return (_modules != NULL); }
 399 
 400   // Loaded class dictionary
 401   Dictionary* dictionary() const { return _dictionary; }
 402 
 403   void add_to_deallocate_list(Metadata* m);





 404 


 405   static ClassLoaderData* class_loader_data(oop loader);
 406   static ClassLoaderData* class_loader_data_or_null(oop loader);
 407   static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);
 408   static void print_loader(ClassLoaderData *loader_data, outputStream *out);
 409 
 410   TRACE_DEFINE_TRACE_ID_METHODS;
 411 };
 412 
 413 // An iterator that distributes Klasses to parallel worker threads.
 414 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj {

 415  Klass* volatile _next_klass;
 416  public:
 417   ClassLoaderDataGraphKlassIteratorAtomic();
 418   Klass* next_klass();
 419  private:
 420   static Klass* next_klass_in_cldg(Klass* klass);
 421 };
 422 
 423 class ClassLoaderDataGraphMetaspaceIterator : public StackObj {

 424   ClassLoaderData* _data;
 425  public:
 426   ClassLoaderDataGraphMetaspaceIterator();
 427   ~ClassLoaderDataGraphMetaspaceIterator();
 428   bool repeat() { return _data != NULL; }
 429   Metaspace* get_next() {
 430     assert(_data != NULL, "Should not be NULL in call to the iterator");
 431     Metaspace* result = _data->metaspace_or_null();
 432     _data = _data->next();
 433     // This result might be NULL for class loaders without metaspace
 434     // yet.  It would be nice to return only non-null results but
 435     // there is no guarantee that there will be a non-null result
 436     // down the list so the caller is going to have to check.
 437     return result;
 438   }
 439 };

 440 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP


   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
  26 #define SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
  27 
  28 #include "memory/allocation.hpp"



  29 #include "oops/oopHandle.hpp"
  30 #include "runtime/handles.hpp"
  31 #include "trace/traceMacros.hpp"
  32 #include "utilities/growableArray.hpp"
  33 #include "utilities/macros.hpp"



  34 
  35 //
  36 // A class loader represents a linkset. Conceptually, a linkset identifies
  37 // the complete transitive closure of resolved links that a dynamic linker can
  38 // produce.
  39 //
  40 // A ClassLoaderData also encapsulates the allocation space, called a metaspace,
  41 // used by the dynamic linker to allocate the runtime representation of all
  42 // the types it defines.
  43 //
  44 // ClassLoaderData are stored in the runtime representation of classes,
  45 // and provides iterators for root tracing and other GC operations.
  46 
  47 class ClassLoaderData;
  48 class Dictionary;
  49 class JNIMethodBlock;
  50 class Metaspace;
  51 class ModuleEntry;

  52 class ModuleEntryTable;
  53 class Mutex;
  54 class PackageEntry;
  55 class PackageEntryTable;


  56 
  57 // GC root for walking class loader data created
  58 
  59 class ClassLoaderDataGraph : public AllStatic {
  60   friend class ClassLoaderData;

  61   friend class ClassLoaderDataGraphKlassIteratorAtomic;
  62   friend class ClassLoaderDataGraphKlassIteratorStatic;
  63   friend class ClassLoaderDataGraphMetaspaceIterator;
  64   friend class VMStructs;
  65  private:
  66   // All CLDs (except the null CLD) can be reached by walking _head->_next->...
  67   static ClassLoaderData* _head;
  68   static ClassLoaderData* _unloading;
  69   // CMS support.
  70   static ClassLoaderData* _saved_head;
  71   static ClassLoaderData* _saved_unloading;
  72   static bool _should_purge;
  73   // OOM has been seen in metaspace allocation. Used to prevent some
  74   // allocations until class unloading
  75   static bool _metaspace_oom;
  76 
  77   static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS);

  78  public:
  79   static ClassLoaderData* find_or_create(Handle class_loader, TRAPS);
  80   static void purge();
  81   static void clear_claimed_marks();
  82   // oops do
  83   static void oops_do(OopClosure* f, bool must_claim);
  84   static void keep_alive_oops_do(OopClosure* blk, bool must_claim);
  85   static void always_strong_oops_do(OopClosure* blk, bool must_claim);
  86   // cld do
  87   static void cld_do(CLDClosure* cl);
  88   static void cld_unloading_do(CLDClosure* cl);
  89   static void roots_cld_do(CLDClosure* strong, CLDClosure* weak);
  90   static void keep_alive_cld_do(CLDClosure* cl);
  91   static void always_strong_cld_do(CLDClosure* cl);
  92   // klass do
  93   // Walking classes through the ClassLoaderDataGraph include array classes.  It also includes
  94   // classes that are allocated but not loaded, classes that have errors, and scratch classes
  95   // for redefinition.  These classes are removed during the next class unloading.
  96   // Walking the ClassLoaderDataGraph also includes anonymous classes.
  97   static void classes_do(KlassClosure* klass_closure);


 125   // CMS support.
 126   static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
 127   static GrowableArray<ClassLoaderData*>* new_clds();
 128 
 129   static void set_should_purge(bool b) { _should_purge = b; }
 130   static void purge_if_needed() {
 131     // Only purge the CLDG for CMS if concurrent sweep is complete.
 132     if (_should_purge) {
 133       purge();
 134       // reset for next time.
 135       set_should_purge(false);
 136     }
 137   }
 138 
 139   static int resize_if_needed();
 140 
 141   static bool has_metaspace_oom()           { return _metaspace_oom; }
 142   static void set_metaspace_oom(bool value) { _metaspace_oom = value; }
 143 
 144   static void dump_on(outputStream * const out) PRODUCT_RETURN;




 145   static bool unload_list_contains(const void* x);



 146 
 147   NOT_PRODUCT(static void verify();)
 148   NOT_PRODUCT(static bool contains_loader_data(ClassLoaderData* loader_data);)



 149 };
 150 
 151 // ClassLoaderData class
 152 
 153 class ClassLoaderData : public CHeapObj<mtClass> {
 154   friend class ClassLoaderDataGraph;
 155   friend class ClassLoaderDataGraphKlassIteratorAtomic;
 156   friend class ClassLoaderDataGraphKlassIteratorStatic;
 157   friend class ClassLoaderDataGraphMetaspaceIterator;
 158   friend class MetaDataFactory;
 159   friend class Method;
 160   friend class Universe;
 161   friend class VMStructs;
 162  private:
 163   class Dependencies VALUE_OBJ_CLASS_SPEC {
 164     friend class ClassLoaderData;
 165     friend class ClassLoaderDataGraph;
 166    private:
 167     objArrayOop _list_head;




 168     Dependencies() : _list_head(NULL) {}
 169     Dependencies(TRAPS) : _list_head(NULL) { init(CHECK); }



 170     void init(TRAPS);
 171     void add(Handle dependency, TRAPS);
 172     void locked_add(objArrayHandle last, objArrayHandle new_dependency, Thread* THREAD);
 173     void oops_do(OopClosure* f);
 174   };
 175 
 176   class ChunkedHandleList VALUE_OBJ_CLASS_SPEC {
 177     friend class ClassLoaderData;
 178    private:
 179     struct Chunk : public CHeapObj<mtClass> {
 180       static const size_t CAPACITY = 32;

 181       oop _data[CAPACITY];
 182       volatile juint _size;
 183       Chunk* _next;
 184       Chunk(Chunk* c) : _next(c), _size(0) {}

 185     };

 186     Chunk* volatile _head;




 187     ChunkedHandleList() : _head(NULL) {}
 188     ~ChunkedHandleList();
 189 
 190     // Only one thread at a time can add, guarded by ClassLoaderData::metaspace_lock().
 191     // However, multiple threads can execute oops_do concurrently with add.
 192     oop* add(oop o);



 193     void oops_do(OopClosure* f);
 194     void oops_do_chunk(OopClosure* f, Chunk* c, const juint size);
 195     DEBUG_ONLY(bool contains(oop* p);)
 196   };
 197 
 198   static ClassLoaderData* _the_null_class_loader_data;





 199 
 200   Dependencies _dependencies; // holds dependencies from this class loader
 201                               // data to others.
 202 
 203   ChunkedHandleList _handles; // Handles to constant pool arrays, Modules, etc, which
 204                               // have the same life cycle of the corresponding ClassLoader.
 205 
 206   oop _class_loader;          // oop used to uniquely identify a class loader
 207                               // class loader or a canonical class path


 208 
 209   Metaspace * volatile _metaspace;  // Meta-space where meta-data defined by the
 210                                     // classes in the class loader are allocated.
 211   Mutex* _metaspace_lock;  // Locks the metaspace for allocations and setup.

















 212 
 213   Klass* volatile _klasses;              // The classes defined by the class loader.
 214   PackageEntryTable* volatile _packages; // The packages defined by the class loader.
 215   ModuleEntryTable*  volatile _modules;  // The modules defined by the class loader.
 216   ModuleEntry* _unnamed_module;          // This class loader's unnamed module.
 217   Dictionary*  _dictionary;              // The loaded InstanceKlasses, including initiated by this class loader
 218 
 219   // These method IDs are created for the class loader and set to NULL when the
 220   // class loader is unloaded.  They are rarely freed, only for redefine classes
 221   // and if they lose a data race in InstanceKlass.
 222   JNIMethodBlock* _jmethod_ids;
 223 
 224   // Metadata to be deallocated when it's safe at class unloading, when
 225   // this class loader isn't unloaded itself.
 226   GrowableArray<Metadata*>* _deallocate_list;
 227 
 228   // Support for walking class loader data objects
 229   ClassLoaderData* _next; /// Next loader_datas created
 230 
 231   s2 _keep_alive;          // if this CLD is kept alive without a keep_alive_object().
 232                            // Used for anonymous classes and the boot class
 233                            // loader. _keep_alive does not need to be volatile or
 234                            // atomic since there is one unique CLD per anonymous class.
 235 
 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 
 240   TRACE_DEFINE_TRACE_ID_FIELD;
 241 
 242   bool _unloading;         // true if this class loader goes away
 243   bool _is_anonymous;      // if this CLD is for an anonymous class
 244 
 245   // Remembered sets support for the oops in the class loader data.
 246   bool _modified_oops;             // Card Table Equivalent (YC/CMS support)
 247   bool _accumulated_modified_oops; // Mod Union Equivalent (CMS support)
 248 
 249   void set_next(ClassLoaderData* next) { _next = next; }
 250   ClassLoaderData* next() const        { return _next; }
 251 
 252   ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies);
 253   ~ClassLoaderData();
 254 
 255   void init_dependencies(TRAPS);
 256 
 257   // Deallocate free list during class unloading.
 258   void free_deallocate_list();
 259   void unload_deallocate_list();    // for the classes that are unloaded
 260 
 261   void unload();
 262   bool keep_alive() const { return _keep_alive > 0; }
 263 
 264   // The CLD are not placed in the Heap, so the Card Table or
 265   // the Mod Union Table can't be used to mark when CLD have modified oops.
 266   // The CT and MUT bits saves this information for the whole class loader data.
 267   void clear_modified_oops() { _modified_oops = false; }








 268 


 269   void classes_do(void f(Klass*));

 270   void classes_do(void f(InstanceKlass*));
 271   void loaded_classes_do(KlassClosure* klass_closure);
 272   void methods_do(void f(Method*));
 273   void modules_do(void f(ModuleEntry*));
 274   void packages_do(void f(PackageEntry*));
 275 








 276  public:
 277   void oops_do(OopClosure* f, bool must_claim, bool clear_modified_oops = false);
 278   void classes_do(KlassClosure* klass_closure);
 279 
 280   void record_modified_oops() { _modified_oops = true; }
 281   bool has_modified_oops() const { return _modified_oops; }































 282 
 283   void accumulate_modified_oops() { if (has_modified_oops()) _accumulated_modified_oops = true; }
 284   void clear_accumulated_modified_oops() { _accumulated_modified_oops = false; }
 285   bool has_accumulated_modified_oops() const { return _accumulated_modified_oops; }
 286 
 287   bool is_alive(BoolObjectClosure* is_alive_closure) const;
 288   // The object the GC is using to keep this ClassLoaderData alive.
 289   oop keep_alive_object() const;
 290   // Used to refcount an anonymous class's CLD in order to
 291   // indicate their aliveness without a keep_alive_object().
 292   void inc_keep_alive();
 293   void dec_keep_alive();
 294 
 295   // Returns true if this class loader data is for a loader going away.
 296   bool is_unloading() const {
 297     assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");
 298     return _unloading;
 299   }
 300 
 301   void clear_claimed() { _claimed = 0; }
 302   bool claimed() const { return _claimed == 1; }
 303   bool claim();

 304 
 305   // The Metaspace is created lazily so may be NULL. This
 306   // method will allocate a Metaspace if needed.
 307   Metaspace* metaspace_non_null();
 308   Mutex* metaspace_lock() const { return _metaspace_lock; }
 309   Metaspace* metaspace_or_null() const { return _metaspace; }
 310 
 311   bool is_the_null_class_loader_data() const { return this == _the_null_class_loader_data; }
 312   bool is_system_class_loader_data() const;
 313   bool is_platform_class_loader_data() const;
 314   bool is_builtin_class_loader_data() const;
 315   bool is_anonymous() const { return _is_anonymous; }
 316 
 317   oop class_loader() const { return _class_loader; }
 318   const char* loader_name();
 319   unsigned int identity_hash() const { return (unsigned int)(((intptr_t)this) >> 3); }
 320   // Loaded class dictionary
 321   Dictionary* dictionary() { return _dictionary; }
 322 

 323   Klass* klasses() { return _klasses; }
 324   PackageEntryTable* packages() { return _packages; }
 325   ModuleEntry* unnamed_module() { return _unnamed_module; }
 326   ModuleEntryTable* modules();
 327   bool modules_defined() { return (_modules != NULL); }
 328 
 329   JNIMethodBlock* jmethod_ids() const              { return _jmethod_ids; }
 330   void set_jmethod_ids(JNIMethodBlock* new_block)  { _jmethod_ids = new_block; }
 331 








 332   OopHandle add_handle(Handle h);
 333   void remove_handle(OopHandle h);
 334   void init_handle_locked(OopHandle& pd, Handle h);  // used for concurrent access to ModuleEntry::_pd field
 335   void add_class(Klass* k, bool publicize = true);
 336   void remove_class(Klass* k);
 337   bool contains_klass(Klass* k);
 338   void record_dependency(const Klass* to, TRAPS);
 339   void add_to_deallocate_list(Metadata* m);




 340 
 341   TRACE_DEFINE_TRACE_ID_METHODS;

 342 
 343   void print() { print_on(tty); }
 344   void print_on(outputStream* out) const;
 345   void print_value() { print_value_on(tty); }
 346   void print_value_on(outputStream* out) const;
 347   void dump(outputStream * const out) PRODUCT_RETURN;
 348   NOT_PRODUCT(void verify();)
 349 
 350   static void init_null_class_loader_data();
 351   static ClassLoaderData* the_null_class_loader_data() { return _the_null_class_loader_data; }
 352   static ClassLoaderData* class_loader_data(oop loader);
 353   static ClassLoaderData* class_loader_data_or_null(oop loader);
 354   static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);



 355 };
 356 
 357 // An iterator that distributes Klasses to parallel worker threads.
 358 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj {
 359  private:
 360   Klass* volatile _next_klass;
 361  public:
 362   ClassLoaderDataGraphKlassIteratorAtomic();
 363   Klass* next_klass();

 364   static Klass* next_klass_in_cldg(Klass* klass);
 365 };
 366 
 367 class ClassLoaderDataGraphMetaspaceIterator : public StackObj {
 368  private:
 369   ClassLoaderData* _data;
 370  public:
 371   ClassLoaderDataGraphMetaspaceIterator();

 372   bool repeat() { return _data != NULL; }
 373   Metaspace* get_next() {
 374     assert(_data != NULL, "Should not be NULL in call to the iterator");
 375     Metaspace* result = _data->metaspace_or_null();
 376     _data = _data->next();
 377     // This result might be NULL for class loaders without metaspace
 378     // yet.  It would be nice to return only non-null results but
 379     // there is no guarantee that there will be a non-null result
 380     // down the list so the caller is going to have to check.
 381     return result;
 382   }
 383 };
 384 
 385 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP
< prev index next >