1 /*
   2  * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   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 "runtime/mutex.hpp"
  33 #include "utilities/growableArray.hpp"
  34 #include "utilities/macros.hpp"
  35 #if INCLUDE_TRACE
  36 #include "utilities/ticks.hpp"
  37 #endif
  38 
  39 //
  40 // A class loader represents a linkset. Conceptually, a linkset identifies
  41 // the complete transitive closure of resolved links that a dynamic linker can
  42 // produce.
  43 //
  44 // A ClassLoaderData also encapsulates the allocation space, called a metaspace,
  45 // used by the dynamic linker to allocate the runtime representation of all
  46 // the types it defines.
  47 //
  48 // ClassLoaderData are stored in the runtime representation of classes and the
  49 // system dictionary, are roots of garbage collection, and provides iterators
  50 // for root tracing and other GC operations.
  51 
  52 class ClassLoaderData;
  53 class JNIMethodBlock;
  54 class JNIHandleBlock;
  55 class Metadebug;
  56 
  57 // GC root for walking class loader data created
  58 
  59 class ClassLoaderDataGraph : public AllStatic {
  60   friend class ClassLoaderData;
  61   friend class ClassLoaderDataGraphMetaspaceIterator;
  62   friend class ClassLoaderDataGraphKlassIteratorAtomic;
  63   friend class VMStructs;
  64  private:
  65   // All CLDs (except the null CLD) can be reached by walking _head->_next->...
  66   static ClassLoaderData* _head;
  67   static ClassLoaderData* _unloading;
  68   // CMS support.
  69   static ClassLoaderData* _saved_head;
  70   static ClassLoaderData* _saved_unloading;
  71   static bool _should_purge;
  72   // OOM has been seen in metaspace allocation. Used to prevent some
  73   // allocations until class unloading
  74   static bool _metaspace_oom;
  75 
  76   static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS);
  77   static void post_class_unload_events(void);
  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, KlassClosure* klass_closure, bool must_claim);
  84   static void keep_alive_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
  85   static void always_strong_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
  86   // cld do
  87   static void cld_do(CLDClosure* cl);
  88   static void roots_cld_do(CLDClosure* strong, CLDClosure* weak);
  89   static void keep_alive_cld_do(CLDClosure* cl);
  90   static void always_strong_cld_do(CLDClosure* cl);
  91   // klass do
  92   static void classes_do(KlassClosure* klass_closure);
  93   static void classes_do(void f(Klass* const));
  94   static void methods_do(void f(Method*));
  95   static void loaded_classes_do(KlassClosure* klass_closure);
  96   static void classes_unloading_do(void f(Klass* const));
  97   static bool do_unloading(BoolObjectClosure* is_alive, bool clean_previous_versions);
  98 
  99   // CMS support.
 100   static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
 101   static GrowableArray<ClassLoaderData*>* new_clds();
 102 
 103   static void set_should_purge(bool b) { _should_purge = b; }
 104   static void purge_if_needed() {
 105     // Only purge the CLDG for CMS if concurrent sweep is complete.
 106     if (_should_purge) {
 107       purge();
 108       // reset for next time.
 109       set_should_purge(false);
 110     }
 111   }
 112 
 113   static bool has_metaspace_oom()           { return _metaspace_oom; }
 114   static void set_metaspace_oom(bool value) { _metaspace_oom = value; }
 115 
 116   static void dump_on(outputStream * const out) PRODUCT_RETURN;
 117   static void dump() { dump_on(tty); }
 118   static void verify();
 119   static void log_creation(Handle loader, ClassLoaderData* cld, TRAPS);
 120 
 121   static bool unload_list_contains(const void* x);
 122 #ifndef PRODUCT
 123   static bool contains_loader_data(ClassLoaderData* loader_data);
 124 #endif
 125 
 126 #if INCLUDE_TRACE
 127  private:
 128   static Ticks _class_unload_time;
 129   static void class_unload_event(Klass* const k);
 130 #endif
 131 };
 132 
 133 // ClassLoaderData class
 134 
 135 class ClassLoaderData : public CHeapObj<mtClass> {
 136   friend class VMStructs;
 137  private:
 138   class Dependencies VALUE_OBJ_CLASS_SPEC {
 139     objArrayOop _list_head;
 140     void locked_add(objArrayHandle last,
 141                     objArrayHandle new_dependency,
 142                     Thread* THREAD);
 143    public:
 144     Dependencies() : _list_head(NULL) {}
 145     Dependencies(TRAPS) : _list_head(NULL) {
 146       init(CHECK);
 147     }
 148     void add(Handle dependency, TRAPS);
 149     void init(TRAPS);
 150     void oops_do(OopClosure* f);
 151   };
 152 
 153   friend class ClassLoaderDataGraph;
 154   friend class ClassLoaderDataGraphKlassIteratorAtomic;
 155   friend class ClassLoaderDataGraphMetaspaceIterator;
 156   friend class MetaDataFactory;
 157   friend class Method;
 158 
 159   static ClassLoaderData * _the_null_class_loader_data;
 160 
 161   oop _class_loader;          // oop used to uniquely identify a class loader
 162                               // class loader or a canonical class path
 163   Dependencies _dependencies; // holds dependencies from this class loader
 164                               // data to others.
 165 
 166   Metaspace * _metaspace;  // Meta-space where meta-data defined by the
 167                            // classes in the class loader are allocated.
 168   Mutex* _metaspace_lock;  // Locks the metaspace for allocations and setup.
 169   bool _unloading;         // true if this class loader goes away
 170   bool _keep_alive;        // if this CLD is kept alive without a keep_alive_object().
 171   bool _is_anonymous;      // if this CLD is for an anonymous class
 172   volatile int _claimed;   // true if claimed, for example during GC traces.
 173                            // To avoid applying oop closure more than once.
 174                            // Has to be an int because we cas it.
 175   Klass* _klasses;         // The classes defined by the class loader.
 176 
 177   JNIHandleBlock* _handles; // Handles to constant pool arrays
 178 
 179   // These method IDs are created for the class loader and set to NULL when the
 180   // class loader is unloaded.  They are rarely freed, only for redefine classes
 181   // and if they lose a data race in InstanceKlass.
 182   JNIMethodBlock*                  _jmethod_ids;
 183 
 184   // Metadata to be deallocated when it's safe at class unloading, when
 185   // this class loader isn't unloaded itself.
 186   GrowableArray<Metadata*>*      _deallocate_list;
 187 
 188   // Support for walking class loader data objects
 189   ClassLoaderData* _next; /// Next loader_datas created
 190 
 191   // CDS
 192   int _shared_class_loader_id;
 193 
 194   // ReadOnly and ReadWrite metaspaces (static because only on the null
 195   // class loader for now).
 196   static Metaspace* _ro_metaspace;
 197   static Metaspace* _rw_metaspace;
 198 
 199   void set_next(ClassLoaderData* next) { _next = next; }
 200   ClassLoaderData* next() const        { return _next; }
 201 
 202   ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies);
 203   ~ClassLoaderData();
 204 
 205   void set_metaspace(Metaspace* m) { _metaspace = m; }
 206 
 207   JNIHandleBlock* handles() const;
 208   void set_handles(JNIHandleBlock* handles);
 209 
 210   // GC interface.
 211   void clear_claimed()          { _claimed = 0; }
 212   bool claimed() const          { return _claimed == 1; }
 213   bool claim();
 214 
 215   void unload();
 216   bool keep_alive() const       { return _keep_alive; }
 217   void classes_do(void f(Klass*));
 218   void loaded_classes_do(KlassClosure* klass_closure);
 219   void classes_do(void f(InstanceKlass*));
 220   void methods_do(void f(Method*));
 221 
 222   // Deallocate free list during class unloading.
 223   void free_deallocate_list();
 224 
 225   // Allocate out of this class loader data
 226   MetaWord* allocate(size_t size);
 227 
 228  public:
 229 
 230   bool is_alive(BoolObjectClosure* is_alive_closure) const;
 231 
 232   // Accessors
 233   Metaspace* metaspace_or_null() const     { return _metaspace; }
 234 
 235   static ClassLoaderData* the_null_class_loader_data() {
 236     return _the_null_class_loader_data;
 237   }
 238 
 239   Mutex* metaspace_lock() const { return _metaspace_lock; }
 240 
 241   bool is_anonymous() const { return _is_anonymous; }
 242 
 243   static void init_null_class_loader_data() {
 244     assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
 245     assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
 246 
 247     // We explicitly initialize the Dependencies object at a later phase in the initialization
 248     _the_null_class_loader_data = new ClassLoaderData((oop)NULL, false, Dependencies());
 249     ClassLoaderDataGraph::_head = _the_null_class_loader_data;
 250     assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
 251     if (DumpSharedSpaces) {
 252       _the_null_class_loader_data->initialize_shared_metaspaces();
 253     }
 254   }
 255 
 256   bool is_the_null_class_loader_data() const {
 257     return this == _the_null_class_loader_data;
 258   }
 259   bool is_ext_class_loader_data() const;
 260 
 261   // The Metaspace is created lazily so may be NULL.  This
 262   // method will allocate a Metaspace if needed.
 263   Metaspace* metaspace_non_null();
 264 
 265   oop class_loader() const      { return _class_loader; }
 266 
 267   // The object the GC is using to keep this ClassLoaderData alive.
 268   oop keep_alive_object() const;
 269 
 270   // Returns true if this class loader data is for a loader going away.
 271   bool is_unloading() const     {
 272     assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");
 273     return _unloading;
 274   }
 275 
 276   // Used to make sure that this CLD is not unloaded.
 277   void set_keep_alive(bool value) { _keep_alive = value; }
 278 
 279   inline unsigned int identity_hash() const;
 280 
 281   // Used when tracing from klasses.
 282   void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
 283 
 284   void classes_do(KlassClosure* klass_closure);
 285 
 286   JNIMethodBlock* jmethod_ids() const              { return _jmethod_ids; }
 287   void set_jmethod_ids(JNIMethodBlock* new_block)  { _jmethod_ids = new_block; }
 288 
 289   void print_value() { print_value_on(tty); }
 290   void print_value_on(outputStream* out) const;
 291   void dump(outputStream * const out) PRODUCT_RETURN;
 292   void verify();
 293   const char* loader_name();
 294 
 295   jobject add_handle(Handle h);
 296   void add_class(Klass* k, bool publicize = true);
 297   void remove_class(Klass* k);
 298   bool contains_klass(Klass* k);
 299   void record_dependency(const Klass* to, TRAPS);
 300   void init_dependencies(TRAPS);
 301 
 302   void add_to_deallocate_list(Metadata* m);
 303 
 304   static ClassLoaderData* class_loader_data(oop loader);
 305   static ClassLoaderData* class_loader_data_or_null(oop loader);
 306   static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);
 307   static void print_loader(ClassLoaderData *loader_data, outputStream *out);
 308 
 309   // CDS support
 310   Metaspace* ro_metaspace();
 311   Metaspace* rw_metaspace();
 312   void initialize_shared_metaspaces();
 313 
 314   int shared_class_loader_id() const {
 315     return _shared_class_loader_id;
 316   }
 317   void set_shared_class_loader_id(int id) {
 318     assert(id >= 0, "sanity");
 319     assert(_shared_class_loader_id <0, "cannot be assigned more than once");
 320     _shared_class_loader_id = id;
 321   }
 322 };
 323 
 324 // An iterator that distributes Klasses to parallel worker threads.
 325 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj {
 326  Klass* volatile _next_klass;
 327  public:
 328   ClassLoaderDataGraphKlassIteratorAtomic();
 329   Klass* next_klass();
 330  private:
 331   static Klass* next_klass_in_cldg(Klass* klass);
 332 };
 333 
 334 class ClassLoaderDataGraphMetaspaceIterator : public StackObj {
 335   ClassLoaderData* _data;
 336  public:
 337   ClassLoaderDataGraphMetaspaceIterator();
 338   ~ClassLoaderDataGraphMetaspaceIterator();
 339   bool repeat() { return _data != NULL; }
 340   Metaspace* get_next() {
 341     assert(_data != NULL, "Should not be NULL in call to the iterator");
 342     Metaspace* result = _data->metaspace_or_null();
 343     _data = _data->next();
 344     // This result might be NULL for class loaders without metaspace
 345     // yet.  It would be nice to return only non-null results but
 346     // there is no guarantee that there will be a non-null result
 347     // down the list so the caller is going to have to check.
 348     return result;
 349   }
 350 };
 351 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP