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 ClassLoaderDataGraph; 57 class JNIMethodBlock; 58 class ModuleEntry; 59 class PackageEntry; 60 class ModuleEntryTable; 61 class PackageEntryTable; 62 class DictionaryEntry; 63 class Dictionary; 64 65 // ClassLoaderData class 66 67 class ClassLoaderData : public CHeapObj<mtClass> { 68 friend class VMStructs; 69 70 private: 71 class ChunkedHandleList { 72 struct Chunk : public CHeapObj<mtClass> { 73 static const size_t CAPACITY = 32; 74 75 oop _data[CAPACITY]; 76 volatile juint _size; 77 Chunk* _next; 78 79 Chunk(Chunk* c) : _size(0), _next(c) { } 80 }; 81 82 Chunk* volatile _head; 83 84 void oops_do_chunk(OopClosure* f, Chunk* c, const juint size); 85 86 public: 87 ChunkedHandleList() : _head(NULL) {} 88 ~ChunkedHandleList(); 89 90 // Only one thread at a time can add, guarded by ClassLoaderData::metaspace_lock(). 91 // However, multiple threads can execute oops_do concurrently with add. 92 oop* add(oop o); 93 bool contains(oop p); 94 NOT_PRODUCT(bool owner_of(oop* p);) 95 void oops_do(OopClosure* f); 96 97 int count() const; 98 }; 99 100 friend class ClassLoaderDataGraph; 101 friend class ClassLoaderDataGraphIterator; 102 friend class ClassLoaderDataGraphKlassIteratorAtomic; 103 friend class ClassLoaderDataGraphKlassIteratorStatic; 104 friend class ClassLoaderDataGraphMetaspaceIterator; 105 friend class Klass; 106 friend class MetaDataFactory; 107 friend class Method; 108 109 static ClassLoaderData * _the_null_class_loader_data; 110 111 WeakHandle<vm_class_loader_data> _holder; // The oop that determines lifetime of this class loader 112 OopHandle _class_loader; // The instance of java/lang/ClassLoader associated with 113 // this ClassLoaderData 114 115 ClassLoaderMetaspace * volatile _metaspace; // Meta-space where meta-data defined by the 116 // classes in the class loader are allocated. 117 Mutex* _metaspace_lock; // Locks the metaspace for allocations and setup. 118 bool _unloading; // true if this class loader goes away 119 bool _is_unsafe_anonymous; // CLD is dedicated to one class and that class determines the CLDs lifecycle. 120 // For example, an unsafe anonymous class. 121 122 // Remembered sets support for the oops in the class loader data. 123 bool _modified_oops; // Card Table Equivalent (YC/CMS support) 124 bool _accumulated_modified_oops; // Mod Union Equivalent (CMS support) 125 126 s2 _keep_alive; // if this CLD is kept alive. 127 // Used for unsafe anonymous classes and the boot class 128 // loader. _keep_alive does not need to be volatile or 129 // atomic since there is one unique CLD per unsafe anonymous class. 130 131 volatile int _claimed; // true if claimed, for example during GC traces. 132 // To avoid applying oop closure more than once. 133 // Has to be an int because we cas it. 134 ChunkedHandleList _handles; // Handles to constant pool arrays, Modules, etc, which 135 // have the same life cycle of the corresponding ClassLoader. 136 137 NOT_PRODUCT(volatile int _dependency_count;) // number of class loader dependencies 138 139 Klass* volatile _klasses; // The classes defined by the class loader. 140 PackageEntryTable* volatile _packages; // The packages defined by the class loader. 141 ModuleEntryTable* volatile _modules; // The modules defined by the class loader. 142 ModuleEntry* _unnamed_module; // This class loader's unnamed module. 143 Dictionary* _dictionary; // The loaded InstanceKlasses, including initiated by this class loader 144 145 // These method IDs are created for the class loader and set to NULL when the 146 // class loader is unloaded. They are rarely freed, only for redefine classes 147 // and if they lose a data race in InstanceKlass. 148 JNIMethodBlock* _jmethod_ids; 149 150 // Metadata to be deallocated when it's safe at class unloading, when 151 // this class loader isn't unloaded itself. 152 GrowableArray<Metadata*>* _deallocate_list; 153 154 // Support for walking class loader data objects 155 ClassLoaderData* _next; /// Next loader_datas created 156 157 Klass* _class_loader_klass; 158 Symbol* _name; 159 Symbol* _name_and_id; 160 JFR_ONLY(DEFINE_TRACE_ID_FIELD;) 161 162 void set_next(ClassLoaderData* next) { _next = next; } 163 ClassLoaderData* next() const { return _next; } 164 165 ClassLoaderData(Handle h_class_loader, bool is_unsafe_anonymous); 166 ~ClassLoaderData(); 167 168 // The CLD are not placed in the Heap, so the Card Table or 169 // the Mod Union Table can't be used to mark when CLD have modified oops. 170 // The CT and MUT bits saves this information for the whole class loader data. 171 void clear_modified_oops() { _modified_oops = false; } 172 public: 173 void record_modified_oops() { _modified_oops = true; } 174 bool has_modified_oops() { return _modified_oops; } 175 176 void accumulate_modified_oops() { if (has_modified_oops()) _accumulated_modified_oops = true; } 177 void clear_accumulated_modified_oops() { _accumulated_modified_oops = false; } 178 bool has_accumulated_modified_oops() { return _accumulated_modified_oops; } 179 oop holder_no_keepalive() const; 180 181 private: 182 void unload(); 183 bool keep_alive() const { return _keep_alive > 0; } 184 185 oop holder_phantom() const; 186 void classes_do(void f(Klass*)); 187 void loaded_classes_do(KlassClosure* klass_closure); 188 void classes_do(void f(InstanceKlass*)); 189 void methods_do(void f(Method*)); 190 void modules_do(void f(ModuleEntry*)); 191 void packages_do(void f(PackageEntry*)); 192 193 // Deallocate free list during class unloading. 194 void free_deallocate_list(); // for the classes that are not unloaded 195 void free_deallocate_list_C_heap_structures(); // for the classes that are unloaded 196 197 // Allocate out of this class loader data 198 MetaWord* allocate(size_t size); 199 200 Dictionary* create_dictionary(); 201 202 void initialize_name(Handle class_loader); 203 public: 204 // GC interface. 205 void clear_claimed() { _claimed = 0; } 206 bool claimed() const { return _claimed == 1; } 207 bool claim(); 208 209 // Computes if the CLD is alive or not. This is safe to call in concurrent 210 // contexts. 211 bool is_alive() const; 212 213 // Accessors 214 ClassLoaderMetaspace* metaspace_or_null() const { return _metaspace; } 215 216 static ClassLoaderData* the_null_class_loader_data() { 217 return _the_null_class_loader_data; 218 } 219 220 Mutex* metaspace_lock() const { return _metaspace_lock; } 221 222 bool is_unsafe_anonymous() const { return _is_unsafe_anonymous; } 223 224 static void init_null_class_loader_data(); 225 226 bool is_the_null_class_loader_data() const { 227 return this == _the_null_class_loader_data; 228 } 229 230 // Returns true if this class loader data is for the system class loader. 231 // (Note that the class loader data may be unsafe anonymous.) 232 bool is_system_class_loader_data() const; 233 234 // Returns true if this class loader data is for the platform class loader. 235 // (Note that the class loader data may be unsafe anonymous.) 236 bool is_platform_class_loader_data() const; 237 238 // Returns true if this class loader data is for the boot class loader. 239 // (Note that the class loader data may be unsafe anonymous.) 240 inline bool is_boot_class_loader_data() const; 241 242 bool is_builtin_class_loader_data() const; 243 bool is_permanent_class_loader_data() const; 244 245 // The Metaspace is created lazily so may be NULL. This 246 // method will allocate a Metaspace if needed. 247 ClassLoaderMetaspace* metaspace_non_null(); 248 249 inline oop class_loader() const; 250 251 // Returns true if this class loader data is for a loader going away. 252 // Note that this is only safe after the GC has computed if the CLD is 253 // unloading or not. In concurrent contexts where there are no such 254 // guarantees, is_alive() should be used instead. 255 bool is_unloading() const { 256 assert(!(is_the_null_class_loader_data() && _unloading), "The null class loader can never be unloaded"); 257 return _unloading; 258 } 259 260 // Used to refcount an unsafe anonymous class's CLD in order to 261 // indicate their aliveness. 262 void inc_keep_alive(); 263 void dec_keep_alive(); 264 265 void initialize_holder(Handle holder); 266 267 void oops_do(OopClosure* f, bool must_claim, bool clear_modified_oops = false); 268 269 void classes_do(KlassClosure* klass_closure); 270 Klass* klasses() { return _klasses; } 271 272 JNIMethodBlock* jmethod_ids() const { return _jmethod_ids; } 273 void set_jmethod_ids(JNIMethodBlock* new_block) { _jmethod_ids = new_block; } 274 275 void print() { print_on(tty); } 276 void print_on(outputStream* out) const PRODUCT_RETURN; 277 void print_value() { print_value_on(tty); } 278 void print_value_on(outputStream* out) const; 279 void verify(); 280 281 OopHandle add_handle(Handle h); 282 void remove_handle(OopHandle h); 283 void init_handle_locked(OopHandle& pd, Handle h); // used for concurrent access to ModuleEntry::_pd field 284 void add_class(Klass* k, bool publicize = true); 285 void remove_class(Klass* k); 286 bool contains_klass(Klass* k); 287 void record_dependency(const Klass* to); 288 PackageEntryTable* packages() { return _packages; } 289 ModuleEntry* unnamed_module() { return _unnamed_module; } 290 ModuleEntryTable* modules(); 291 bool modules_defined() { return (_modules != NULL); } 292 293 // Loaded class dictionary 294 Dictionary* dictionary() const { return _dictionary; } 295 296 void add_to_deallocate_list(Metadata* m); 297 298 static ClassLoaderData* class_loader_data(oop loader); 299 static ClassLoaderData* class_loader_data_or_null(oop loader); 300 static ClassLoaderData* unsafe_anonymous_class_loader_data(Handle loader); 301 302 // Returns Klass* of associated class loader, or NULL if associated loader is 'bootstrap'. 303 // Also works if unloading. 304 Klass* class_loader_klass() const { return _class_loader_klass; } 305 306 // Returns the class loader's explict name as specified during 307 // construction or the class loader's qualified class name. 308 // Works during unloading. 309 const char* loader_name() const; 310 // Returns the explicitly specified class loader name or NULL. 311 Symbol* name() const { return _name; } 312 313 // Obtain the class loader's _name_and_id, works during unloading. 314 const char* loader_name_and_id() const; 315 Symbol* name_and_id() const { return _name_and_id; } 316 317 JFR_ONLY(DEFINE_TRACE_ID_METHODS;) 318 }; 319 320 #endif // SHARE_VM_CLASSFILE_CLASSLOADERDATA_HPP