1 /*
   2  * Copyright (c) 2012, 2013, 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 
  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 and the
  45 // system dictionary, are roots of garbage collection, and provides iterators
  46 // for root tracing and other GC operations.
  47 
  48 class ClassLoaderData;
  49 class JNIMethodBlock;
  50 class JNIHandleBlock;
  51 class Metadebug;
  52 // GC root for walking class loader data created
  53 
  54 class ClassLoaderDataGraph : public AllStatic {
  55   friend class ClassLoaderData;
  56   friend class ClassLoaderDataGraphMetaspaceIterator;
  57   friend class VMStructs;
  58  private:
  59   // All CLDs (except the null CLD) can be reached by walking _head->_next->...
  60   static ClassLoaderData* _head;
  61   static ClassLoaderData* _unloading;
  62   // CMS support.
  63   static ClassLoaderData* _saved_head;
  64 
  65   static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS);
  66  public:
  67   static ClassLoaderData* find_or_create(Handle class_loader, TRAPS);
  68   static void purge();
  69   static void clear_claimed_marks();
  70   static void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
  71   static void always_strong_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
  72   static void keep_alive_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim);
  73   static void classes_do(KlassClosure* klass_closure);
  74   static bool do_unloading(BoolObjectClosure* is_alive);
  75 
  76   // CMS support.
  77   static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); }
  78   static GrowableArray<ClassLoaderData*>* new_clds();
  79 
  80   static void dump_on(outputStream * const out) PRODUCT_RETURN;
  81   static void dump() { dump_on(tty); }
  82   static void verify();
  83 
  84 #ifndef PRODUCT
  85   // expensive test for pointer in metaspace for debugging
  86   static bool contains(address x);
  87   static bool contains_loader_data(ClassLoaderData* loader_data);
  88 #endif
  89 };
  90 
  91 // ClassLoaderData class
  92 
  93 class ClassLoaderData : public CHeapObj<mtClass> {
  94   friend class VMStructs;
  95  private:
  96   class Dependencies VALUE_OBJ_CLASS_SPEC {
  97     objArrayOop _list_head;
  98     void locked_add(objArrayHandle last,
  99                     objArrayHandle new_dependency,
 100                     Thread* THREAD);
 101    public:
 102     Dependencies() : _list_head(NULL) {}
 103     Dependencies(TRAPS) : _list_head(NULL) {
 104       init(CHECK);
 105     }
 106     void add(Handle dependency, TRAPS);
 107     void init(TRAPS);
 108     void oops_do(OopClosure* f);
 109   };
 110 
 111   friend class ClassLoaderDataGraph;
 112   friend class ClassLoaderDataGraphMetaspaceIterator;
 113   friend class MetaDataFactory;
 114   friend class Method;
 115 
 116   static ClassLoaderData * _the_null_class_loader_data;
 117 
 118   oop _class_loader;          // oop used to uniquely identify a class loader
 119                               // class loader or a canonical class path
 120   Dependencies _dependencies; // holds dependencies from this class loader
 121                               // data to others.
 122 
 123   Metaspace * _metaspace;  // Meta-space where meta-data defined by the
 124                            // classes in the class loader are allocated.
 125   Mutex* _metaspace_lock;  // Locks the metaspace for allocations and setup.
 126   bool _unloading;         // true if this class loader goes away
 127   bool _keep_alive;        // if this CLD can be unloaded for anonymous loaders
 128   bool _is_anonymous;      // if this CLD is for an anonymous class
 129   volatile int _claimed;   // true if claimed, for example during GC traces.
 130                            // To avoid applying oop closure more than once.
 131                            // Has to be an int because we cas it.
 132   Klass* _klasses;         // The classes defined by the class loader.
 133 
 134   JNIHandleBlock* _handles; // Handles to constant pool arrays
 135 
 136   // These method IDs are created for the class loader and set to NULL when the
 137   // class loader is unloaded.  They are rarely freed, only for redefine classes
 138   // and if they lose a data race in InstanceKlass.
 139   JNIMethodBlock*                  _jmethod_ids;
 140 
 141   // Metadata to be deallocated when it's safe at class unloading, when
 142   // this class loader isn't unloaded itself.
 143   GrowableArray<Metadata*>*      _deallocate_list;
 144 
 145   // Support for walking class loader data objects
 146   ClassLoaderData* _next; /// Next loader_datas created
 147 
 148   // ReadOnly and ReadWrite metaspaces (static because only on the null
 149   // class loader for now).
 150   static Metaspace* _ro_metaspace;
 151   static Metaspace* _rw_metaspace;
 152 
 153   void set_next(ClassLoaderData* next) { _next = next; }
 154   ClassLoaderData* next() const        { return _next; }
 155 
 156   ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies);
 157   ~ClassLoaderData();
 158 
 159   void set_metaspace(Metaspace* m) { _metaspace = m; }
 160 
 161   JNIHandleBlock* handles() const;
 162   void set_handles(JNIHandleBlock* handles);
 163 
 164   Mutex* metaspace_lock() const { return _metaspace_lock; }
 165 
 166   // GC interface.
 167   void clear_claimed()          { _claimed = 0; }
 168   bool claimed() const          { return _claimed == 1; }
 169   bool claim();
 170 
 171   void unload();
 172   bool keep_alive() const       { return _keep_alive; }
 173   bool is_alive(BoolObjectClosure* is_alive_closure) const;
 174 
 175   void classes_do(void f(InstanceKlass*));
 176 
 177   // Deallocate free list during class unloading.
 178   void free_deallocate_list();
 179 
 180   // Allocate out of this class loader data
 181   MetaWord* allocate(size_t size);
 182 
 183  public:
 184   // Accessors
 185   Metaspace* metaspace_or_null() const     { return _metaspace; }
 186 
 187   static ClassLoaderData* the_null_class_loader_data() {
 188     return _the_null_class_loader_data;
 189   }
 190 
 191   bool is_anonymous() const { return _is_anonymous; }
 192 
 193   static void init_null_class_loader_data() {
 194     assert(_the_null_class_loader_data == NULL, "cannot initialize twice");
 195     assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice");
 196 
 197     // We explicitly initialize the Dependencies object at a later phase in the initialization
 198     _the_null_class_loader_data = new ClassLoaderData((oop)NULL, false, Dependencies());
 199     ClassLoaderDataGraph::_head = _the_null_class_loader_data;
 200     assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be");
 201     if (DumpSharedSpaces) {
 202       _the_null_class_loader_data->initialize_shared_metaspaces();
 203     }
 204   }
 205 
 206   bool is_the_null_class_loader_data() const {
 207     return this == _the_null_class_loader_data;
 208   }
 209   bool is_ext_class_loader_data() const;
 210 
 211   // The Metaspace is created lazily so may be NULL.  This
 212   // method will allocate a Metaspace if needed.
 213   Metaspace* metaspace_non_null();
 214 
 215   oop class_loader() const      { return _class_loader; }
 216 
 217   // Returns true if this class loader data is for a loader going away.
 218   bool is_unloading() const     {
 219     assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded");
 220     return _unloading;
 221   }
 222   // Anonymous class loader data doesn't have anything to keep them from
 223   // being unloaded during parsing the anonymous class.
 224   void set_keep_alive(bool value) { _keep_alive = value; }
 225 
 226   unsigned int identity_hash() {
 227     return _class_loader == NULL ? 0 : _class_loader->identity_hash();
 228   }
 229 
 230   // Used when tracing from klasses.
 231   void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim);
 232 
 233   void classes_do(KlassClosure* klass_closure);
 234 
 235   JNIMethodBlock* jmethod_ids() const              { return _jmethod_ids; }
 236   void set_jmethod_ids(JNIMethodBlock* new_block)  { _jmethod_ids = new_block; }
 237 
 238   void print_value() { print_value_on(tty); }
 239   void print_value_on(outputStream* out) const;
 240   void dump(outputStream * const out) PRODUCT_RETURN;
 241   void verify();
 242   const char* loader_name();
 243 
 244   jobject add_handle(Handle h);
 245   void add_class(Klass* k);
 246   void remove_class(Klass* k);
 247   void record_dependency(Klass* to, TRAPS);
 248   void init_dependencies(TRAPS);
 249 
 250   void add_to_deallocate_list(Metadata* m);
 251 
 252   static ClassLoaderData* class_loader_data(oop loader);
 253   static ClassLoaderData* class_loader_data_or_null(oop loader);
 254   static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS);
 255   static void print_loader(ClassLoaderData *loader_data, outputStream *out);
 256 
 257   // CDS support
 258   Metaspace* ro_metaspace();
 259   Metaspace* rw_metaspace();
 260   void initialize_shared_metaspaces();
 261 };
 262 
 263 class ClassLoaderDataGraphMetaspaceIterator : public StackObj {
 264   ClassLoaderData* _data;
 265  public:
 266   ClassLoaderDataGraphMetaspaceIterator();
 267   ~ClassLoaderDataGraphMetaspaceIterator();
 268   bool repeat() { return _data != NULL; }
 269   Metaspace* get_next() {
 270     assert(_data != NULL, "Should not be NULL in call to the iterator");
 271     Metaspace* result = _data->metaspace_or_null();
 272     _data = _data->next();
 273     // This result might be NULL for class loaders without metaspace
 274     // yet.  It would be nice to return only non-null results but
 275     // there is no guarantee that there will be a non-null result
 276     // down the list so the caller is going to have to check.
 277     return result;
 278   }
 279 };
 280 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP