1 /* 2 * Copyright (c) 2012, 2018, 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 "oops/oopHandle.hpp" 32 #include "oops/weakHandle.hpp" 33 #include "runtime/mutex.hpp" 34 #include "utilities/growableArray.hpp" 35 #include "utilities/macros.hpp" 36 #if INCLUDE_JFR 37 #include "jfr/support/jfrTraceIdExtension.hpp" 38 #endif 39 40 // external name (synthetic) for the primordial "bootstrap" class loader instance 41 #define BOOTSTRAP_LOADER_NAME "bootstrap" 42 #define BOOTSTRAP_LOADER_NAME_LEN 9 43 44 // 45 // A class loader represents a linkset. Conceptually, a linkset identifies 46 // the complete transitive closure of resolved links that a dynamic linker can 47 // produce. 48 // 49 // A ClassLoaderData also encapsulates the allocation space, called a metaspace, 50 // used by the dynamic linker to allocate the runtime representation of all 51 // the types it defines. 52 // 53 // ClassLoaderData are stored in the runtime representation of classes, 54 // and provides iterators for root tracing and other GC operations. 55 56 class ClassLoaderData; 57 class JNIMethodBlock; 58 class Metadebug; 59 class ModuleEntry; 60 class PackageEntry; 61 class ModuleEntryTable; 62 class PackageEntryTable; 63 class DictionaryEntry; 64 class Dictionary; 65 66 // GC root for walking class loader data created 67 68 class ClassLoaderDataGraph : public AllStatic { 69 friend class ClassLoaderData; 70 friend class ClassLoaderDataGraphMetaspaceIterator; 71 friend class ClassLoaderDataGraphKlassIteratorAtomic; 72 friend class ClassLoaderDataGraphKlassIteratorStatic; 73 friend class ClassLoaderDataGraphIterator; 74 friend class VMStructs; 75 private: 76 // All CLDs (except the null CLD) can be reached by walking _head->_next->... 77 static ClassLoaderData* _head; 78 static ClassLoaderData* _unloading; 79 // CMS support. 80 static ClassLoaderData* _saved_head; 81 static ClassLoaderData* _saved_unloading; 82 static bool _should_purge; 83 84 // Set if there's anything to purge in the deallocate lists or previous versions 85 // during a safepoint after class unloading in a full GC. 86 static bool _should_clean_deallocate_lists; 87 static bool _safepoint_cleanup_needed; 88 89 // OOM has been seen in metaspace allocation. Used to prevent some 90 // allocations until class unloading 91 static bool _metaspace_oom; 92 93 static volatile size_t _num_instance_classes; 94 static volatile size_t _num_array_classes; 95 96 static ClassLoaderData* add_to_graph(Handle class_loader, bool is_unsafe_anonymous); 97 static ClassLoaderData* add(Handle class_loader, bool is_unsafe_anonymous); 98 99 public: 100 static ClassLoaderData* find_or_create(Handle class_loader); 101 static void clean_module_and_package_info(); 102 static void purge(); 103 static void clear_claimed_marks(); 104 // Iteration through CLDG inside a safepoint; GC support 105 static void cld_do(CLDClosure* cl); 106 static void cld_unloading_do(CLDClosure* cl); 107 static void roots_cld_do(CLDClosure* strong, CLDClosure* weak); 108 static void always_strong_cld_do(CLDClosure* cl); 109 // klass do 110 // Walking classes through the ClassLoaderDataGraph include array classes. It also includes 111 // classes that are allocated but not loaded, classes that have errors, and scratch classes 112 // for redefinition. These classes are removed during the next class unloading. 113 // Walking the ClassLoaderDataGraph also includes unsafe anonymous classes. 114 static void classes_do(KlassClosure* klass_closure); 115 static void classes_do(void f(Klass* const)); 116 static void methods_do(void f(Method*)); 117 static void modules_do(void f(ModuleEntry*)); 118 static void modules_unloading_do(void f(ModuleEntry*)); 119 static void packages_do(void f(PackageEntry*)); 120 static void packages_unloading_do(void f(PackageEntry*)); 121 static void loaded_classes_do(KlassClosure* klass_closure); 122 static void unlocked_loaded_classes_do(KlassClosure* klass_closure); 123 static void classes_unloading_do(void f(Klass* const)); 124 static bool do_unloading(bool do_cleaning); 125 126 // Expose state to avoid logging overhead in safepoint cleanup tasks. 127 static inline bool should_clean_metaspaces_and_reset(); 128 static void set_should_clean_deallocate_lists() { _should_clean_deallocate_lists = true; } 129 static void clean_deallocate_lists(bool purge_previous_versions); 130 static void walk_metadata_and_clean_metaspaces(); 131 132 // dictionary do 133 // Iterate over all klasses in dictionary, but 134 // just the classes from defining class loaders. 135 static void dictionary_classes_do(void f(InstanceKlass*)); 136 // Added for initialize_itable_for_klass to handle exceptions. 137 static void dictionary_classes_do(void f(InstanceKlass*, TRAPS), TRAPS); 138 139 // VM_CounterDecay iteration support 140 static InstanceKlass* try_get_next_class(); 141 142 static void verify_dictionary(); 143 static void print_dictionary(outputStream* st); 144 static void print_dictionary_statistics(outputStream* st); 145 146 // CMS support. 147 static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); } 148 static GrowableArray<ClassLoaderData*>* new_clds(); 149 150 static void set_should_purge(bool b) { _should_purge = b; } 151 static void purge_if_needed() { 152 // Only purge the CLDG for CMS if concurrent sweep is complete. 153 if (_should_purge) { 154 purge(); 155 // reset for next time. 156 set_should_purge(false); 157 } 158 } 159 160 static int resize_if_needed(); 161 162 static bool has_metaspace_oom() { return _metaspace_oom; } 163 static void set_metaspace_oom(bool value) { _metaspace_oom = value; } 164 165 static void print_on(outputStream * const out) PRODUCT_RETURN; 166 static void print() { print_on(tty); } 167 static void verify(); 168 169 // instance and array class counters 170 static inline size_t num_instance_classes(); 171 static inline size_t num_array_classes(); 172 static inline void inc_instance_classes(size_t count); 173 static inline void dec_instance_classes(size_t count); 174 static inline void inc_array_classes(size_t count); 175 static inline void dec_array_classes(size_t count); 176 177 #ifndef PRODUCT 178 static bool contains_loader_data(ClassLoaderData* loader_data); 179 #endif 180 }; 181 182 class LockedClassesDo : public KlassClosure { 183 typedef void (*classes_do_func_t)(Klass*); 184 classes_do_func_t _function; 185 public: 186 LockedClassesDo(); // For callers who provide their own do_klass 187 LockedClassesDo(classes_do_func_t function); 188 ~LockedClassesDo(); 189 190 void do_klass(Klass* k) { 191 (*_function)(k); 192 } 193 }; 194 195 196 // ClassLoaderData class 197 198 class ClassLoaderData : public CHeapObj<mtClass> { 199 friend class VMStructs; 200 201 private: 202 class ChunkedHandleList { 203 struct Chunk : public CHeapObj<mtClass> { 204 static const size_t CAPACITY = 32; 205 206 oop _data[CAPACITY]; 207 volatile juint _size; 208 Chunk* _next; 209 210 Chunk(Chunk* c) : _size(0), _next(c) { } 211 }; 212 213 Chunk* volatile _head; 214 215 void oops_do_chunk(OopClosure* f, Chunk* c, const juint size); 216 217 public: 218 ChunkedHandleList() : _head(NULL) {} 219 ~ChunkedHandleList(); 220 221 // Only one thread at a time can add, guarded by ClassLoaderData::metaspace_lock(). 222 // However, multiple threads can execute oops_do concurrently with add. 223 oop* add(oop o); 224 bool contains(oop p); 225 NOT_PRODUCT(bool owner_of(oop* p);) 226 void oops_do(OopClosure* f); 227 228 int count() const; 229 }; 230 231 friend class ClassLoaderDataGraph; 232 friend class ClassLoaderDataGraphIterator; 233 friend class ClassLoaderDataGraphKlassIteratorAtomic; 234 friend class ClassLoaderDataGraphKlassIteratorStatic; 235 friend class ClassLoaderDataGraphMetaspaceIterator; 236 friend class Klass; 237 friend class MetaDataFactory; 238 friend class Method; 239 240 static ClassLoaderData * _the_null_class_loader_data; 241 242 WeakHandle<vm_class_loader_data> _holder; // The oop that determines lifetime of this class loader 243 OopHandle _class_loader; // The instance of java/lang/ClassLoader associated with 244 // this ClassLoaderData 245 246 ClassLoaderMetaspace * volatile _metaspace; // Meta-space where meta-data defined by the 247 // classes in the class loader are allocated. 248 Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup. 249 bool _unloading; // true if this class loader goes away 250 bool _is_unsafe_anonymous; // CLD is dedicated to one class and that class determines the CLDs lifecycle. 251 // For example, an unsafe anonymous class. 252 253 // Remembered sets support for the oops in the class loader data. 254 bool _modified_oops; // Card Table Equivalent (YC/CMS support) 255 bool _accumulated_modified_oops; // Mod Union Equivalent (CMS support) 256 257 s2 _keep_alive; // if this CLD is kept alive. 258 // Used for unsafe anonymous classes and the boot class 259 // loader. _keep_alive does not need to be volatile or 260 // atomic since there is one unique CLD per unsafe anonymous class. 261 262 volatile int _claim; // non-zero if claimed, for example during GC traces. 263 // To avoid applying oop closure more than once. 264 ChunkedHandleList _handles; // Handles to constant pool arrays, Modules, etc, which 265 // have the same life cycle of the corresponding ClassLoader. 266 267 NOT_PRODUCT(volatile int _dependency_count;) // number of class loader dependencies 268 269 Klass* volatile _klasses; // The classes defined by the class loader. 270 PackageEntryTable* volatile _packages; // The packages defined by the class loader. 271 ModuleEntryTable* volatile _modules; // The modules defined by the class loader. 272 ModuleEntry* _unnamed_module; // This class loader's unnamed module. 273 Dictionary* _dictionary; // The loaded InstanceKlasses, including initiated by this class loader 274 275 // These method IDs are created for the class loader and set to NULL when the 276 // class loader is unloaded. They are rarely freed, only for redefine classes 277 // and if they lose a data race in InstanceKlass. 278 JNIMethodBlock* _jmethod_ids; 279 280 // Metadata to be deallocated when it's safe at class unloading, when 281 // this class loader isn't unloaded itself. 282 GrowableArray<Metadata*>* _deallocate_list; 283 284 // Support for walking class loader data objects 285 ClassLoaderData* _next; /// Next loader_datas created 286 287 Klass* _class_loader_klass; 288 Symbol* _name; 289 Symbol* _name_and_id; 290 JFR_ONLY(DEFINE_TRACE_ID_FIELD;) 291 292 void set_next(ClassLoaderData* next) { _next = next; } 293 ClassLoaderData* next() const { return _next; } 294 295 ClassLoaderData(Handle h_class_loader, bool is_unsafe_anonymous); 296 ~ClassLoaderData(); 297 298 // The CLD are not placed in the Heap, so the Card Table or 299 // the Mod Union Table can't be used to mark when CLD have modified oops. 300 // The CT and MUT bits saves this information for the whole class loader data. 301 void clear_modified_oops() { _modified_oops = false; } 302 public: 303 void record_modified_oops() { _modified_oops = true; } 304 bool has_modified_oops() { return _modified_oops; } 305 306 void accumulate_modified_oops() { if (has_modified_oops()) _accumulated_modified_oops = true; } 307 void clear_accumulated_modified_oops() { _accumulated_modified_oops = false; } 308 bool has_accumulated_modified_oops() { return _accumulated_modified_oops; } 309 oop holder_no_keepalive() const; 310 311 private: 312 void unload(); 313 bool keep_alive() const { return _keep_alive > 0; } 314 315 oop holder_phantom() const; 316 void classes_do(void f(Klass*)); 317 void loaded_classes_do(KlassClosure* klass_closure); 318 void classes_do(void f(InstanceKlass*)); 319 void methods_do(void f(Method*)); 320 void modules_do(void f(ModuleEntry*)); 321 void packages_do(void f(PackageEntry*)); 322 323 // Deallocate free list during class unloading. 324 void free_deallocate_list(); // for the classes that are not unloaded 325 void free_deallocate_list_C_heap_structures(); // for the classes that are unloaded 326 327 // Allocate out of this class loader data 328 MetaWord* allocate(size_t size); 329 330 Dictionary* create_dictionary(); 331 332 void initialize_name(Handle class_loader); 333 334 public: 335 // GC interface. 336 337 // The "claim" is typically used to check if oops_do needs to be applied on 338 // the CLD or not. Most GCs only perform strong marking during the marking phase. 339 enum { 340 _claim_none = 0, 341 _claim_finalizable = 2, 342 _claim_strong = 3 343 }; 344 void clear_claim() { _claim = 0; } 345 bool claimed() const { return _claim != 0; } 346 bool try_claim(int claim); 347 int get_claim() const { return _claim; } 348 void set_claim(int claim) { _claim = claim; } 349 350 // Computes if the CLD is alive or not. This is safe to call in concurrent 351 // contexts. 352 bool is_alive() const; 353 354 // Accessors 355 ClassLoaderMetaspace* metaspace_or_null() const { return _metaspace; } 356 357 static ClassLoaderData* the_null_class_loader_data() { 358 return _the_null_class_loader_data; 359 } 360 361 Mutex* metaspace_lock() const { return _metaspace_lock; } 362 363 bool is_unsafe_anonymous() const { return _is_unsafe_anonymous; } 364 365 static void init_null_class_loader_data(); 366 367 bool is_the_null_class_loader_data() const { 368 return this == _the_null_class_loader_data; 369 } 370 371 // Returns true if this class loader data is for the system class loader. 372 // (Note that the class loader data may be unsafe anonymous.) 373 bool is_system_class_loader_data() const; 374 375 // Returns true if this class loader data is for the platform class loader. 376 // (Note that the class loader data may be unsafe anonymous.) 377 bool is_platform_class_loader_data() const; 378 379 // Returns true if this class loader data is for the boot class loader. 380 // (Note that the class loader data may be unsafe anonymous.) 381 inline bool is_boot_class_loader_data() const; 382 383 bool is_builtin_class_loader_data() const; 384 bool is_permanent_class_loader_data() const; 385 386 // The Metaspace is created lazily so may be NULL. This 387 // method will allocate a Metaspace if needed. 388 ClassLoaderMetaspace* metaspace_non_null(); 389 390 inline oop class_loader() const; 391 392 // Returns true if this class loader data is for a loader going away. 393 // Note that this is only safe after the GC has computed if the CLD is 394 // unloading or not. In concurrent contexts where there are no such 395 // guarantees, is_alive() should be used instead. 396 bool is_unloading() const { 397 assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded"); 398 return _unloading; 399 } 400 401 // Used to refcount an unsafe anonymous class's CLD in order to 402 // indicate their aliveness. 403 void inc_keep_alive(); 404 void dec_keep_alive(); 405 406 void initialize_holder(Handle holder); 407 408 void oops_do(OopClosure* f, int claim_value, bool clear_modified_oops = false); 409 410 void classes_do(KlassClosure* klass_closure); 411 Klass* klasses() { return _klasses; } 412 413 JNIMethodBlock* jmethod_ids() const { return _jmethod_ids; } 414 void set_jmethod_ids(JNIMethodBlock* new_block) { _jmethod_ids = new_block; } 415 416 void print() { print_on(tty); } 417 void print_on(outputStream* out) const PRODUCT_RETURN; 418 void print_value() { print_value_on(tty); } 419 void print_value_on(outputStream* out) const; 420 void verify(); 421 422 OopHandle add_handle(Handle h); 423 void remove_handle(OopHandle h); 424 void init_handle_locked(OopHandle& pd, Handle h); // used for concurrent access to ModuleEntry::_pd field 425 void add_class(Klass* k, bool publicize = true); 426 void remove_class(Klass* k); 427 bool contains_klass(Klass* k); 428 void record_dependency(const Klass* to); 429 PackageEntryTable* packages() { return _packages; } 430 ModuleEntry* unnamed_module() { return _unnamed_module; } 431 ModuleEntryTable* modules(); 432 bool modules_defined() { return (_modules != NULL); } 433 434 // Loaded class dictionary 435 Dictionary* dictionary() const { return _dictionary; } 436 437 void add_to_deallocate_list(Metadata* m); 438 439 static ClassLoaderData* class_loader_data(oop loader); 440 static ClassLoaderData* class_loader_data_or_null(oop loader); 441 static ClassLoaderData* unsafe_anonymous_class_loader_data(Handle loader); 442 443 // Returns Klass* of associated class loader, or NULL if associated loader is 'bootstrap'. 444 // Also works if unloading. 445 Klass* class_loader_klass() const { return _class_loader_klass; } 446 447 // Returns the class loader's explict name as specified during 448 // construction or the class loader's qualified class name. 449 // Works during unloading. 450 const char* loader_name() const; 451 // Returns the explicitly specified class loader name or NULL. 452 Symbol* name() const { return _name; } 453 454 // Obtain the class loader's _name_and_id, works during unloading. 455 const char* loader_name_and_id() const; 456 Symbol* name_and_id() const { return _name_and_id; } 457 458 JFR_ONLY(DEFINE_TRACE_ID_METHODS;) 459 }; 460 461 // An iterator that distributes Klasses to parallel worker threads. 462 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj { 463 Klass* volatile _next_klass; 464 public: 465 ClassLoaderDataGraphKlassIteratorAtomic(); 466 Klass* next_klass(); 467 private: 468 static Klass* next_klass_in_cldg(Klass* klass); 469 }; 470 471 class ClassLoaderDataGraphMetaspaceIterator : public StackObj { 472 ClassLoaderData* _data; 473 public: 474 ClassLoaderDataGraphMetaspaceIterator(); 475 ~ClassLoaderDataGraphMetaspaceIterator(); 476 bool repeat() { return _data != NULL; } 477 ClassLoaderMetaspace* get_next() { 478 assert(_data != NULL, "Should not be NULL in call to the iterator"); 479 ClassLoaderMetaspace* result = _data->metaspace_or_null(); 480 _data = _data->next(); 481 // This result might be NULL for class loaders without metaspace 482 // yet. It would be nice to return only non-null results but 483 // there is no guarantee that there will be a non-null result 484 // down the list so the caller is going to have to check. 485 return result; 486 } 487 }; 488 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP