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