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 VMStructs; 74 private: 75 // All CLDs (except the null CLD) can be reached by walking _head->_next->... 76 static ClassLoaderData* _head; 77 static ClassLoaderData* _unloading; 78 // CMS support. 79 static ClassLoaderData* _saved_head; 80 static ClassLoaderData* _saved_unloading; 81 static bool _should_purge; 82 // OOM has been seen in metaspace allocation. Used to prevent some 83 // allocations until class unloading 84 static bool _metaspace_oom; 85 86 static volatile size_t _num_instance_classes; 87 static volatile size_t _num_array_classes; 88 89 static ClassLoaderData* add_to_graph(Handle class_loader, bool anonymous); 90 static ClassLoaderData* add(Handle class_loader, bool anonymous); 91 public: 92 static ClassLoaderData* find_or_create(Handle class_loader); 93 static void purge(); 94 static void clear_claimed_marks(); 95 // oops do 96 static void oops_do(OopClosure* f, bool must_claim); 97 static void keep_alive_oops_do(OopClosure* blk, bool must_claim); 98 static void always_strong_oops_do(OopClosure* blk, bool must_claim); 99 // cld do 100 static void cld_do(CLDClosure* cl); 101 static void cld_unloading_do(CLDClosure* cl); 102 static void roots_cld_do(CLDClosure* strong, CLDClosure* weak); 103 static void keep_alive_cld_do(CLDClosure* cl); 104 static void always_strong_cld_do(CLDClosure* cl); 105 // klass do 106 // Walking classes through the ClassLoaderDataGraph include array classes. It also includes 107 // classes that are allocated but not loaded, classes that have errors, and scratch classes 108 // for redefinition. These classes are removed during the next class unloading. 109 // Walking the ClassLoaderDataGraph also includes anonymous classes. 110 static void classes_do(KlassClosure* klass_closure); 111 static void classes_do(void f(Klass* const)); 112 static void methods_do(void f(Method*)); 113 static void modules_do(void f(ModuleEntry*)); 114 static void modules_unloading_do(void f(ModuleEntry*)); 115 static void packages_do(void f(PackageEntry*)); 116 static void packages_unloading_do(void f(PackageEntry*)); 117 static void loaded_classes_do(KlassClosure* klass_closure); 118 static void classes_unloading_do(void f(Klass* const)); 119 static bool do_unloading(bool clean_previous_versions); 120 121 // dictionary do 122 // Iterate over all klasses in dictionary, but 123 // just the classes from defining class loaders. 124 static void dictionary_classes_do(void f(InstanceKlass*)); 125 // Added for initialize_itable_for_klass to handle exceptions. 126 static void dictionary_classes_do(void f(InstanceKlass*, TRAPS), TRAPS); 127 128 // Iterate all classes and their class loaders, including initiating class loaders. 129 static void dictionary_all_entries_do(void f(InstanceKlass*, ClassLoaderData*)); 130 131 // VM_CounterDecay iteration support 132 static InstanceKlass* try_get_next_class(); 133 134 static void verify_dictionary(); 135 static void print_dictionary(outputStream* st); 136 static void print_dictionary_statistics(outputStream* st); 137 138 // CMS support. 139 static void remember_new_clds(bool remember) { _saved_head = (remember ? _head : NULL); } 140 static GrowableArray<ClassLoaderData*>* new_clds(); 141 142 static void set_should_purge(bool b) { _should_purge = b; } 143 static void purge_if_needed() { 144 // Only purge the CLDG for CMS if concurrent sweep is complete. 145 if (_should_purge) { 146 purge(); 147 // reset for next time. 148 set_should_purge(false); 149 } 150 } 151 152 static int resize_if_needed(); 153 154 static bool has_metaspace_oom() { return _metaspace_oom; } 155 static void set_metaspace_oom(bool value) { _metaspace_oom = value; } 156 157 static void print_on(outputStream * const out) PRODUCT_RETURN; 158 static void print() { print_on(tty); } 159 static void verify(); 160 161 // instance and array class counters 162 static inline size_t num_instance_classes(); 163 static inline size_t num_array_classes(); 164 static inline void inc_instance_classes(size_t count); 165 static inline void dec_instance_classes(size_t count); 166 static inline void inc_array_classes(size_t count); 167 static inline void dec_array_classes(size_t count); 168 169 #ifndef PRODUCT 170 static bool contains_loader_data(ClassLoaderData* loader_data); 171 #endif 172 }; 173 174 // ClassLoaderData class 175 176 class ClassLoaderData : public CHeapObj<mtClass> { 177 friend class VMStructs; 178 179 private: 180 class ChunkedHandleList { 181 struct Chunk : public CHeapObj<mtClass> { 182 static const size_t CAPACITY = 32; 183 184 oop _data[CAPACITY]; 185 volatile juint _size; 186 Chunk* _next; 187 188 Chunk(Chunk* c) : _next(c), _size(0) { } 189 }; 190 191 Chunk* volatile _head; 192 193 void oops_do_chunk(OopClosure* f, Chunk* c, const juint size); 194 195 public: 196 ChunkedHandleList() : _head(NULL) {} 197 ~ChunkedHandleList(); 198 199 // Only one thread at a time can add, guarded by ClassLoaderData::metaspace_lock(). 200 // However, multiple threads can execute oops_do concurrently with add. 201 oop* add(oop o); 202 bool contains(oop p); 203 NOT_PRODUCT(bool owner_of(oop* p);) 204 void oops_do(OopClosure* f); 205 206 int count() const; 207 }; 208 209 friend class ClassLoaderDataGraph; 210 friend class ClassLoaderDataGraphKlassIteratorAtomic; 211 friend class ClassLoaderDataGraphKlassIteratorStatic; 212 friend class ClassLoaderDataGraphMetaspaceIterator; 213 friend class InstanceKlass; 214 friend class MetaDataFactory; 215 friend class Method; 216 217 static ClassLoaderData * _the_null_class_loader_data; 218 219 WeakHandle<vm_class_loader_data> _holder; // The oop that determines lifetime of this class loader 220 OopHandle _class_loader; // The instance of java/lang/ClassLoader associated with 221 // this ClassLoaderData 222 223 ClassLoaderMetaspace * volatile _metaspace; // Meta-space where meta-data defined by the 224 // classes in the class loader are allocated. 225 Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup. 226 bool _unloading; // true if this class loader goes away 227 bool _is_anonymous; // if this CLD is for an anonymous class 228 229 // Remembered sets support for the oops in the class loader data. 230 bool _modified_oops; // Card Table Equivalent (YC/CMS support) 231 bool _accumulated_modified_oops; // Mod Union Equivalent (CMS support) 232 233 s2 _keep_alive; // if this CLD is kept alive. 234 // Used for anonymous classes and the boot class 235 // loader. _keep_alive does not need to be volatile or 236 // atomic since there is one unique CLD per anonymous class. 237 238 volatile int _claimed; // true if claimed, for example during GC traces. 239 // To avoid applying oop closure more than once. 240 // Has to be an int because we cas it. 241 ChunkedHandleList _handles; // Handles to constant pool arrays, Modules, etc, which 242 // have the same life cycle of the corresponding ClassLoader. 243 244 NOT_PRODUCT(volatile int _dependency_count;) // number of class loader dependencies 245 246 Klass* volatile _klasses; // The classes defined by the class loader. 247 PackageEntryTable* volatile _packages; // The packages defined by the class loader. 248 ModuleEntryTable* volatile _modules; // The modules defined by the class loader. 249 ModuleEntry* _unnamed_module; // This class loader's unnamed module. 250 Dictionary* _dictionary; // The loaded InstanceKlasses, including initiated by this class loader 251 252 // These method IDs are created for the class loader and set to NULL when the 253 // class loader is unloaded. They are rarely freed, only for redefine classes 254 // and if they lose a data race in InstanceKlass. 255 JNIMethodBlock* _jmethod_ids; 256 257 // Metadata to be deallocated when it's safe at class unloading, when 258 // this class loader isn't unloaded itself. 259 GrowableArray<Metadata*>* _deallocate_list; 260 261 // Support for walking class loader data objects 262 ClassLoaderData* _next; /// Next loader_datas created 263 264 Klass* _class_loader_klass; 265 Symbol* _name; 266 Symbol* _name_and_id; 267 JFR_ONLY(DEFINE_TRACE_ID_FIELD;) 268 269 void set_next(ClassLoaderData* next) { _next = next; } 270 ClassLoaderData* next() const { return _next; } 271 272 ClassLoaderData(Handle h_class_loader, bool is_anonymous); 273 ~ClassLoaderData(); 274 275 // The CLD are not placed in the Heap, so the Card Table or 276 // the Mod Union Table can't be used to mark when CLD have modified oops. 277 // The CT and MUT bits saves this information for the whole class loader data. 278 void clear_modified_oops() { _modified_oops = false; } 279 public: 280 void record_modified_oops() { _modified_oops = true; } 281 bool has_modified_oops() { return _modified_oops; } 282 283 void accumulate_modified_oops() { if (has_modified_oops()) _accumulated_modified_oops = true; } 284 void clear_accumulated_modified_oops() { _accumulated_modified_oops = false; } 285 bool has_accumulated_modified_oops() { return _accumulated_modified_oops; } 286 private: 287 288 void unload(); 289 bool keep_alive() const { return _keep_alive > 0; } 290 291 oop holder_phantom() const; 292 void classes_do(void f(Klass*)); 293 void loaded_classes_do(KlassClosure* klass_closure); 294 void classes_do(void f(InstanceKlass*)); 295 void methods_do(void f(Method*)); 296 void modules_do(void f(ModuleEntry*)); 297 void packages_do(void f(PackageEntry*)); 298 299 // Deallocate free list during class unloading. 300 void free_deallocate_list(); // for the classes that are not unloaded 301 void unload_deallocate_list(); // for the classes that are unloaded 302 303 // Allocate out of this class loader data 304 MetaWord* allocate(size_t size); 305 306 Dictionary* create_dictionary(); 307 308 void initialize_name(Handle class_loader); 309 public: 310 // GC interface. 311 void clear_claimed() { _claimed = 0; } 312 bool claimed() const { return _claimed == 1; } 313 bool claim(); 314 315 bool is_alive() const; 316 317 // Accessors 318 ClassLoaderMetaspace* metaspace_or_null() const { return _metaspace; } 319 320 static ClassLoaderData* the_null_class_loader_data() { 321 return _the_null_class_loader_data; 322 } 323 324 Mutex* metaspace_lock() const { return _metaspace_lock; } 325 326 bool is_anonymous() const { return _is_anonymous; } 327 328 static void init_null_class_loader_data(); 329 330 bool is_the_null_class_loader_data() const { 331 return this == _the_null_class_loader_data; 332 } 333 334 // Returns true if this class loader data is for the system class loader. 335 // (Note that the class loader data may be anonymous.) 336 bool is_system_class_loader_data() const; 337 338 // Returns true if this class loader data is for the platform class loader. 339 // (Note that the class loader data may be anonymous.) 340 bool is_platform_class_loader_data() const; 341 342 // Returns true if this class loader data is for the boot class loader. 343 // (Note that the class loader data may be anonymous.) 344 inline bool is_boot_class_loader_data() const; 345 346 bool is_builtin_class_loader_data() const; 347 bool is_permanent_class_loader_data() const; 348 349 // The Metaspace is created lazily so may be NULL. This 350 // method will allocate a Metaspace if needed. 351 ClassLoaderMetaspace* metaspace_non_null(); 352 353 inline oop class_loader() const; 354 355 // Returns true if this class loader data is for a loader going away. 356 bool is_unloading() const { 357 assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded"); 358 return _unloading; 359 } 360 361 // Used to refcount an anonymous class's CLD in order to 362 // indicate their aliveness. 363 void inc_keep_alive(); 364 void dec_keep_alive(); 365 366 void initialize_holder(Handle holder); 367 368 void oops_do(OopClosure* f, bool must_claim, bool clear_modified_oops = false); 369 370 void classes_do(KlassClosure* klass_closure); 371 Klass* klasses() { return _klasses; } 372 373 JNIMethodBlock* jmethod_ids() const { return _jmethod_ids; } 374 void set_jmethod_ids(JNIMethodBlock* new_block) { _jmethod_ids = new_block; } 375 376 void print() { print_on(tty); } 377 void print_on(outputStream* out) const PRODUCT_RETURN; 378 void print_value() { print_value_on(tty); } 379 void print_value_on(outputStream* out) const; 380 void verify(); 381 382 OopHandle add_handle(Handle h); 383 void remove_handle(OopHandle h); 384 void init_handle_locked(OopHandle& pd, Handle h); // used for concurrent access to ModuleEntry::_pd field 385 void add_class(Klass* k, bool publicize = true); 386 void remove_class(Klass* k); 387 bool contains_klass(Klass* k); 388 void record_dependency(const Klass* to); 389 PackageEntryTable* packages() { return _packages; } 390 ModuleEntry* unnamed_module() { return _unnamed_module; } 391 ModuleEntryTable* modules(); 392 bool modules_defined() { return (_modules != NULL); } 393 394 // Loaded class dictionary 395 Dictionary* dictionary() const { return _dictionary; } 396 397 void add_to_deallocate_list(Metadata* m); 398 399 static ClassLoaderData* class_loader_data(oop loader); 400 static ClassLoaderData* class_loader_data_or_null(oop loader); 401 static ClassLoaderData* anonymous_class_loader_data(Handle loader); 402 403 // Returns Klass* of associated class loader, or NULL if associated loader is 'bootstrap'. 404 // Also works if unloading. 405 Klass* class_loader_klass() const { return _class_loader_klass; } 406 407 // Returns the class loader's explict name as specified during 408 // construction or the class loader's qualified class name. 409 // Works during unloading. 410 const char* loader_name() const; 411 // Returns the explicitly specified class loader name or NULL. 412 Symbol* name() const { return _name; } 413 414 // Obtain the class loader's _name_and_id, works during unloading. 415 const char* loader_name_and_id() const; 416 Symbol* name_and_id() const { return _name_and_id; } 417 418 JFR_ONLY(DEFINE_TRACE_ID_METHODS;) 419 }; 420 421 // An iterator that distributes Klasses to parallel worker threads. 422 class ClassLoaderDataGraphKlassIteratorAtomic : public StackObj { 423 Klass* volatile _next_klass; 424 public: 425 ClassLoaderDataGraphKlassIteratorAtomic(); 426 Klass* next_klass(); 427 private: 428 static Klass* next_klass_in_cldg(Klass* klass); 429 }; 430 431 class ClassLoaderDataGraphMetaspaceIterator : public StackObj { 432 ClassLoaderData* _data; 433 public: 434 ClassLoaderDataGraphMetaspaceIterator(); 435 ~ClassLoaderDataGraphMetaspaceIterator(); 436 bool repeat() { return _data != NULL; } 437 ClassLoaderMetaspace* get_next() { 438 assert(_data != NULL, "Should not be NULL in call to the iterator"); 439 ClassLoaderMetaspace* result = _data->metaspace_or_null(); 440 _data = _data->next(); 441 // This result might be NULL for class loaders without metaspace 442 // yet. It would be nice to return only non-null results but 443 // there is no guarantee that there will be a non-null result 444 // down the list so the caller is going to have to check. 445 return result; 446 } 447 }; 448 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP