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