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 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 ClassLoaderDataGraphKlassIteratorAtomic; 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 static ClassLoaderData* _saved_unloading; 70 static bool _should_purge; 71 72 static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS); 73 static void post_class_unload_events(void); 74 static void clean_metaspaces(); 75 public: 76 static ClassLoaderData* find_or_create(Handle class_loader, TRAPS); 77 static void purge(); 78 static void clear_claimed_marks(); 79 // oops do 80 static void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim); 81 static void keep_alive_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim); 82 static void always_strong_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim); 83 // cld do 84 static void cld_do(CLDClosure* cl); 85 static void roots_cld_do(CLDClosure* strong, CLDClosure* weak); 86 static void keep_alive_cld_do(CLDClosure* cl); 87 static void always_strong_cld_do(CLDClosure* cl); 88 // klass do 89 static void classes_do(KlassClosure* klass_closure); 90 static void classes_do(void f(Klass* const)); 91 static void loaded_classes_do(KlassClosure* klass_closure); 92 static void classes_unloading_do(void f(Klass* const)); 93 static bool do_unloading(BoolObjectClosure* is_alive, bool clean_alive); 94 95 // CMS support. 96 static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); } 97 static GrowableArray<ClassLoaderData*>* new_clds(); 98 99 static void set_should_purge(bool b) { _should_purge = b; } 100 static void purge_if_needed() { 101 // Only purge the CLDG for CMS if concurrent sweep is complete. 102 if (_should_purge) { 103 purge(); 104 // reset for next time. 105 set_should_purge(false); 106 } 107 } 108 109 static void free_deallocate_lists(); 110 111 static void dump_on(outputStream * const out) PRODUCT_RETURN; 112 static void dump() { dump_on(tty); } 113 static void verify(); 114 115 static bool unload_list_contains(const void* x); 116 #ifndef PRODUCT 117 static bool contains_loader_data(ClassLoaderData* loader_data); 118 #endif 119 120 #if INCLUDE_TRACE 121 private: 122 static Ticks _class_unload_time; 123 static void class_unload_event(Klass* const k); 124 #endif 125 }; 126 127 // ClassLoaderData class 128 129 class ClassLoaderData : public CHeapObj<mtClass> { 130 friend class VMStructs; 131 private: 132 class Dependencies VALUE_OBJ_CLASS_SPEC { 133 objArrayOop _list_head; 134 void locked_add(objArrayHandle last, 135 objArrayHandle new_dependency, 136 Thread* THREAD); 137 public: 138 Dependencies() : _list_head(NULL) {} 139 Dependencies(TRAPS) : _list_head(NULL) { 140 init(CHECK); 141 } 142 void add(Handle dependency, TRAPS); 143 void init(TRAPS); 144 void oops_do(OopClosure* f); 196 ChunkedHandleList _handles; // Handles to constant pool arrays, etc, which 197 // have the same life cycle of the corresponding ClassLoader. 198 199 // These method IDs are created for the class loader and set to NULL when the 200 // class loader is unloaded. They are rarely freed, only for redefine classes 201 // and if they lose a data race in InstanceKlass. 202 JNIMethodBlock* _jmethod_ids; 203 204 // Metadata to be deallocated when it's safe at class unloading, when 205 // this class loader isn't unloaded itself. 206 GrowableArray<Metadata*>* _deallocate_list; 207 208 // Support for walking class loader data objects 209 ClassLoaderData* _next; /// Next loader_datas created 210 211 // ReadOnly and ReadWrite metaspaces (static because only on the null 212 // class loader for now). 213 static Metaspace* _ro_metaspace; 214 static Metaspace* _rw_metaspace; 215 216 void set_next(ClassLoaderData* next) { _next = next; } 217 ClassLoaderData* next() const { return _next; } 218 219 ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies); 220 ~ClassLoaderData(); 221 222 void set_metaspace(Metaspace* m) { _metaspace = m; } 223 224 Mutex* metaspace_lock() const { return _metaspace_lock; } 225 226 // GC interface. 227 void clear_claimed() { _claimed = 0; } 228 bool claimed() const { return _claimed == 1; } 229 bool claim(); 230 231 void unload(); 232 bool keep_alive() const { return _keep_alive; } 233 void classes_do(void f(Klass*)); 234 void loaded_classes_do(KlassClosure* klass_closure); 235 void classes_do(void f(InstanceKlass*)); 236 237 // Deallocate free list during class unloading. 238 void free_deallocate_list(); 239 240 // Allocate out of this class loader data 241 MetaWord* allocate(size_t size); 242 243 public: 244 245 bool is_alive(BoolObjectClosure* is_alive_closure) const; 246 247 // Accessors 248 Metaspace* metaspace_or_null() const { return _metaspace; } 249 250 static ClassLoaderData* the_null_class_loader_data() { 251 return _the_null_class_loader_data; 252 } 253 254 bool is_anonymous() const { return _is_anonymous; } 255 256 static void init_null_class_loader_data() { 257 assert(_the_null_class_loader_data == NULL, "cannot initialize twice"); 258 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice"); 259 260 // We explicitly initialize the Dependencies object at a later phase in the initialization 261 _the_null_class_loader_data = new ClassLoaderData((oop)NULL, false, Dependencies()); 262 ClassLoaderDataGraph::_head = _the_null_class_loader_data; 263 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be"); 264 if (DumpSharedSpaces) { 308 const char* loader_name(); 309 310 jobject add_handle(Handle h); 311 void add_class(Klass* k); 312 void remove_class(Klass* k); 313 bool contains_klass(Klass* k); 314 void record_dependency(Klass* to, TRAPS); 315 void init_dependencies(TRAPS); 316 317 void add_to_deallocate_list(Metadata* m); 318 319 static ClassLoaderData* class_loader_data(oop loader); 320 static ClassLoaderData* class_loader_data_or_null(oop loader); 321 static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS); 322 static void print_loader(ClassLoaderData *loader_data, outputStream *out); 323 324 // CDS support 325 Metaspace* ro_metaspace(); 326 Metaspace* rw_metaspace(); 327 void initialize_shared_metaspaces(); 328 }; 329 330 // An iterator that distributes Klasses to parallel worker threads. 331 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj { 332 Klass* volatile _next_klass; 333 public: 334 ClassLoaderDataGraphKlassIteratorAtomic(); 335 Klass* next_klass(); 336 private: 337 static Klass* next_klass_in_cldg(Klass* klass); 338 }; 339 340 class ClassLoaderDataGraphMetaspaceIterator : public StackObj { 341 ClassLoaderData* _data; 342 public: 343 ClassLoaderDataGraphMetaspaceIterator(); 344 ~ClassLoaderDataGraphMetaspaceIterator(); 345 bool repeat() { return _data != NULL; } 346 Metaspace* get_next() { 347 assert(_data != NULL, "Should not be NULL in call to the iterator"); | 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_JFR 36 #include "jfr/support/jfrTraceIdExtension.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 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 ClassLoaderDataGraphKlassIteratorAtomic; 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 static ClassLoaderData* _saved_unloading; 70 static bool _should_purge; 71 72 static ClassLoaderData* add(Handle class_loader, bool anonymous, TRAPS); 73 static void clean_metaspaces(); 74 public: 75 static ClassLoaderData* find_or_create(Handle class_loader, TRAPS); 76 static void purge(); 77 static void clear_claimed_marks(); 78 // oops do 79 static void oops_do(OopClosure* f, KlassClosure* klass_closure, bool must_claim); 80 static void keep_alive_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim); 81 static void always_strong_oops_do(OopClosure* blk, KlassClosure* klass_closure, bool must_claim); 82 // cld do 83 static void cld_do(CLDClosure* cl); 84 static void cld_unloading_do(CLDClosure* cl); 85 static void roots_cld_do(CLDClosure* strong, CLDClosure* weak); 86 static void keep_alive_cld_do(CLDClosure* cl); 87 static void always_strong_cld_do(CLDClosure* cl); 88 // klass do 89 static void classes_do(KlassClosure* klass_closure); 90 static void classes_do(void f(Klass* const)); 91 static void loaded_classes_do(KlassClosure* klass_closure); 92 static void classes_unloading_do(void f(Klass* const)); 93 static bool do_unloading(BoolObjectClosure* is_alive, bool clean_alive); 94 95 // CMS support. 96 static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); } 97 static GrowableArray<ClassLoaderData*>* new_clds(); 98 99 static void set_should_purge(bool b) { _should_purge = b; } 100 static void purge_if_needed() { 101 // Only purge the CLDG for CMS if concurrent sweep is complete. 102 if (_should_purge) { 103 purge(); 104 // reset for next time. 105 set_should_purge(false); 106 } 107 } 108 109 static void free_deallocate_lists(); 110 111 static void dump_on(outputStream * const out) PRODUCT_RETURN; 112 static void dump() { dump_on(tty); } 113 static void verify(); 114 115 static bool unload_list_contains(const void* x); 116 #ifndef PRODUCT 117 static bool contains_loader_data(ClassLoaderData* loader_data); 118 #endif 119 }; 120 121 // ClassLoaderData class 122 123 class ClassLoaderData : public CHeapObj<mtClass> { 124 friend class VMStructs; 125 private: 126 class Dependencies VALUE_OBJ_CLASS_SPEC { 127 objArrayOop _list_head; 128 void locked_add(objArrayHandle last, 129 objArrayHandle new_dependency, 130 Thread* THREAD); 131 public: 132 Dependencies() : _list_head(NULL) {} 133 Dependencies(TRAPS) : _list_head(NULL) { 134 init(CHECK); 135 } 136 void add(Handle dependency, TRAPS); 137 void init(TRAPS); 138 void oops_do(OopClosure* f); 190 ChunkedHandleList _handles; // Handles to constant pool arrays, etc, which 191 // have the same life cycle of the corresponding ClassLoader. 192 193 // These method IDs are created for the class loader and set to NULL when the 194 // class loader is unloaded. They are rarely freed, only for redefine classes 195 // and if they lose a data race in InstanceKlass. 196 JNIMethodBlock* _jmethod_ids; 197 198 // Metadata to be deallocated when it's safe at class unloading, when 199 // this class loader isn't unloaded itself. 200 GrowableArray<Metadata*>* _deallocate_list; 201 202 // Support for walking class loader data objects 203 ClassLoaderData* _next; /// Next loader_datas created 204 205 // ReadOnly and ReadWrite metaspaces (static because only on the null 206 // class loader for now). 207 static Metaspace* _ro_metaspace; 208 static Metaspace* _rw_metaspace; 209 210 JFR_ONLY(DEFINE_TRACE_ID_FIELD;) 211 212 void set_next(ClassLoaderData* next) { _next = next; } 213 ClassLoaderData* next() const { return _next; } 214 215 ClassLoaderData(Handle h_class_loader, bool is_anonymous, Dependencies dependencies); 216 ~ClassLoaderData(); 217 218 void set_metaspace(Metaspace* m) { _metaspace = m; } 219 220 Mutex* metaspace_lock() const { return _metaspace_lock; } 221 222 void unload(); 223 bool keep_alive() const { return _keep_alive; } 224 void classes_do(void f(Klass*)); 225 void loaded_classes_do(KlassClosure* klass_closure); 226 void classes_do(void f(InstanceKlass*)); 227 228 // Deallocate free list during class unloading. 229 void free_deallocate_list(); 230 231 // Allocate out of this class loader data 232 MetaWord* allocate(size_t size); 233 234 public: 235 236 // GC interface. 237 void clear_claimed() { _claimed = 0; } 238 bool claimed() const { return _claimed == 1; } 239 bool claim(); 240 241 bool is_alive(BoolObjectClosure* is_alive_closure) const; 242 243 // Accessors 244 Metaspace* metaspace_or_null() const { return _metaspace; } 245 246 static ClassLoaderData* the_null_class_loader_data() { 247 return _the_null_class_loader_data; 248 } 249 250 bool is_anonymous() const { return _is_anonymous; } 251 252 static void init_null_class_loader_data() { 253 assert(_the_null_class_loader_data == NULL, "cannot initialize twice"); 254 assert(ClassLoaderDataGraph::_head == NULL, "cannot initialize twice"); 255 256 // We explicitly initialize the Dependencies object at a later phase in the initialization 257 _the_null_class_loader_data = new ClassLoaderData((oop)NULL, false, Dependencies()); 258 ClassLoaderDataGraph::_head = _the_null_class_loader_data; 259 assert(_the_null_class_loader_data->is_the_null_class_loader_data(), "Must be"); 260 if (DumpSharedSpaces) { 304 const char* loader_name(); 305 306 jobject add_handle(Handle h); 307 void add_class(Klass* k); 308 void remove_class(Klass* k); 309 bool contains_klass(Klass* k); 310 void record_dependency(Klass* to, TRAPS); 311 void init_dependencies(TRAPS); 312 313 void add_to_deallocate_list(Metadata* m); 314 315 static ClassLoaderData* class_loader_data(oop loader); 316 static ClassLoaderData* class_loader_data_or_null(oop loader); 317 static ClassLoaderData* anonymous_class_loader_data(oop loader, TRAPS); 318 static void print_loader(ClassLoaderData *loader_data, outputStream *out); 319 320 // CDS support 321 Metaspace* ro_metaspace(); 322 Metaspace* rw_metaspace(); 323 void initialize_shared_metaspaces(); 324 325 JFR_ONLY(DEFINE_TRACE_ID_METHODS;) 326 }; 327 328 // An iterator that distributes Klasses to parallel worker threads. 329 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj { 330 Klass* volatile _next_klass; 331 public: 332 ClassLoaderDataGraphKlassIteratorAtomic(); 333 Klass* next_klass(); 334 private: 335 static Klass* next_klass_in_cldg(Klass* klass); 336 }; 337 338 class ClassLoaderDataGraphMetaspaceIterator : public StackObj { 339 ClassLoaderData* _data; 340 public: 341 ClassLoaderDataGraphMetaspaceIterator(); 342 ~ClassLoaderDataGraphMetaspaceIterator(); 343 bool repeat() { return _data != NULL; } 344 Metaspace* get_next() { 345 assert(_data != NULL, "Should not be NULL in call to the iterator"); |