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